Skip to content

[TWE-661] Add sticky call to action and new link type for all call to actions #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,509 changes: 1,293 additions & 216 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"dependencies": {
"js-cookie": "^3.0.5",
"lite-youtube-embed": "^0.3.2",
"micromodal": "^0.6.1",
"swiper": "^11.2.1"
}
}
28 changes: 27 additions & 1 deletion tbx/core/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def get_button_link(self):
# Ensure page exists and is live.
if block.value and block.value.live:
return block.value.url
elif block_type == "external_link":
elif block_type == "external_link" or block_type == "modal_iframe":
return block.value
elif block_type == "email":
return f"mailto:{block.value}"
Expand All @@ -248,6 +248,7 @@ class CallToActionBlock(blocks.StructBlock):
("external_link", blocks.URLBlock()),
("email", blocks.EmailBlock()),
("document_link", DocumentChooserBlock()),
("modal_iframe", blocks.URLBlock()),
],
required=True,
max_num=1,
Expand All @@ -268,6 +269,26 @@ class Meta:
template = "patterns/molecules/streamfield/blocks/contact_call_to_action.html"


class StickyCTABlock(blocks.StructBlock):
sticky_text = blocks.CharBlock(max_length=40)
sticky_subtext = blocks.CharBlock(max_length=55)
button_link = blocks.StreamBlock(
[
("internal_link", blocks.PageChooserBlock()),
("external_link", blocks.URLBlock()),
("email", blocks.EmailBlock()),
("document_link", DocumentChooserBlock()),
("modal_iframe", blocks.URLBlock()),
],
required=True,
max_num=1,
)

class Meta:
template = "patterns/molecules/streamfield/blocks/sticky_call_to_action.html"
value_class = ButtonLinkStructValue


class DynamicHeroBlock(blocks.StructBlock):
"""
This block displays text that will be cycled through.
Expand Down Expand Up @@ -1233,6 +1254,11 @@ class StoryBlock(blocks.StreamBlock):
template="patterns/molecules/streamfield/blocks/contact_call_to_action.html",
group="Calls to action",
)
sticky_call_to_action = StickyCTABlock(
label="Sticky Call to Action",
template="patterns/molecules/streamfield/blocks/sticky_call_to_action.html",
group="Calls to action",
)
pullquote = PullQuoteBlock(
template="patterns/molecules/streamfield/blocks/pullquote_block.html",
group="Basics",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "atoms/icon_buttons/icon_button.html" %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<button class="icon-button{% if modifier %} icon-button--{{ modifier }}{% endif %}{% if inverse %} icon-button--inverse{% endif %}{% if hidden %} hidden{% endif %}" {% if modifier and name %}data-{{ name }}-{{ modifier }}{% endif %}{% if data %} {{ data }}{% endif %}{% if aria %} {{ aria }}{% endif %}>
<span class="u-sr-only">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This seems to be redundant as we already have an aria attribute on the button.
For the modal, we pass "Close modal" as an aria label, and we also pass "close" as a modifier, so the screen reader will announce both of these.
Also, names of the icons are usually not descriptive enough to act as proper labels for screen reader users.

{% if modifier %}{{ modifier }}{% endif %}
</span>
{% if modifier == 'next' %}
<span class="rotate">{% include "patterns/atoms/icons/icon.html" with name="chevron" %}</span>
{% elif modifier == 'previous' %}
<span class="rotate rotate--left">{% include "patterns/atoms/icons/icon.html" with name="chevron" %}</span>
{% elif modifier == 'down' %}
<span>{% include "patterns/atoms/icons/icon.html" with name="chevron" %}</span>
{% elif modifier %}
<span>{% include "patterns/atoms/icons/icon.html" with name=modifier %}</span>
{% endif %}
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,40 @@ <h2 class="heading heading--two-b call-to-action__heading">{{ value.text }}</h2>
{% endif %}
</div>
{% if value.button_text and value.button_link %}
<a href="{{ value.get_button_link }}" class="call-to-action__button button">
{{ value.button_text }}
{% if value.get_button_link_block.block_type == "document_link" %}
({{ value.get_button_file_size|filesizeformat }})
{% endif %}
</a>
{% if value.get_button_link_block.block_type == "modal_iframe" %}
<button class="call-to-action__button button" data-micromodal-trigger="iframe-embed-modal">{{ value.button_text }}</button>
{% else %}
<a href="{{ value.get_button_link }}" class="call-to-action__button button">
{{ value.button_text }}
{% if value.get_button_link_block.block_type == "document_link" %}
({{ value.get_button_file_size|filesizeformat }})
{% endif %}
</a>
{% endif %}
{% endif %}
</div>
</div>

{% if value.get_button_link_block.block_type == "modal_iframe" %}
<!-- Modal content -->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: can we create a separate reusable modal component for maintainability? It's the same in all 3 cases.

<div class="modal" id="iframe-embed-modal" aria-hidden="true">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: should use unique IDs if we want to support multiple modals on the page. Otherwise, multiple modals with iframes won't work as expected.

<div class="modal__overlay" data-micromodal-close></div>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-title" >
<header class="modal__header">
<h2 class="modal__heading heading heading--two" id="modal-title">Service Enquiry</h2>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: should we make the modal title configurable instead of hard-coding it? Especially important if we want to support multiple modals on the page

<div class="modal__close">
{% include "patterns/atoms/icon_buttons/icon_button.html" with modifier="close" data="data-micromodal-close" aria='aria-label="Close modal"' %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: It looks like we don't have an icon with the name "close", so nothing is being rendered here. We could either use an existing icon from the sprites file, or add a new one

</div>
</header>
<main class="modal__content" id="filters-content">
<iframe
src="{{ cta.value.get_button_link }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: should use value instead of cta.value, otherwise we have an empty iframe

width="100%"
height="650px"
frameborder="0"
title="Modal Webform">
</iframe>
</main>
</div>
</div>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,40 @@ <h2 class="heading heading--two-b contact-cta__heading">{{ cta.value.text }}</h2

{# CTA button #}
{% if cta.value.button_text and cta.value.button_link %}
<a href="{{ cta.value.get_button_link }}" class="contact-cta__button button">
{{ cta.value.button_text }}
{% if cta.value.get_button_link_block.block_type == "document_link" %}
({{ cta.value.get_button_file_size|filesizeformat }})
{% endif %}
</a>
{% if cta.value.get_button_link_block.block_type == "modal_iframe" %}
<button class="contact-cta__button button" data-micromodal-trigger="iframe-embed-modal">{{ cta.value.button_text }}</button>
{% else %}
<a href="{{ cta.value.get_button_link }}" class="contact-cta__button button">
{{ cta.value.button_text }}
{% if cta.value.get_button_link_block.block_type == "document_link" %}
({{ cta.value.get_button_file_size|filesizeformat }})
{% endif %}
</a>
{% endif %}
{% endif %}
</div>
{% if cta.value.get_button_link_block.block_type == "modal_iframe" %}
<!-- Modal content -->
<div class="modal" id="iframe-embed-modal" aria-hidden="true">
<div class="modal__overlay" data-micromodal-close></div>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-title" >
<header class="modal__header">
<h2 class="modal__heading heading heading--two" id="modal-title">Service Enquiry</h2>
<div class="modal__close">
{% include "patterns/atoms/icon_buttons/icon_button.html" with modifier="close" data="data-micromodal-close" aria='aria-label="Close modal"' %}
</div>
</header>
<main class="modal__content" id="filters-content">
<iframe
src="{{ cta.value.get_button_link }}"
width="100%"
height="650px"
frameborder="0"
title="Modal Webform">
</iframe>
</main>
</div>
</div>
{% endif %}
{% endwith %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% load wagtailcore_tags %}
{# call_to_action is a required field (streamblock) in the block definition #}


<div class="call-to-action__sticky" data-sticky-cta>
<div class="call-to-action__inner">
{% if value.get_button_link_block.block_type == "modal_iframe" %}
<button class="call-to-action__stickybutton button" data-micromodal-trigger="iframe-embed-modal">
<span class="call-to-action__stickyheading">{{ value.sticky_text }}</span>
{% if value.sticky_subtext %}
<br><span class="call-to-action__stickysubtext">{{ value.sticky_subtext }}</span>
{% endif %}
</button>
{% else %}
<a href="{{ value.get_button_link }}" class="call-to-action__button button">
<span class="call-to-action__stickyheading">{{ value.sticky_text }}</span>
{% if value.get_button_link_block.block_type == "document_link" %}
({{ value.get_button_file_size|filesizeformat }})
{% endif %}
{% if value.sticky_subtext %}
<br><span class="call-to-action__stickysubtext">{{ value.sticky_subtext }}</span>
{% endif %}
</a>
{% endif %}
</div>
</div>

{% if value.get_button_link_block.block_type == "modal_iframe" %}
<!-- Modal content -->
<div class="modal" id="iframe-embed-modal" aria-hidden="true">
<div class="modal__overlay" data-micromodal-close></div>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-title" >
<header class="modal__header">
<h2 class="modal__heading heading heading--two" id="modal-title">Service Enquiry</h2>
<div class="modal__close">
{% include "patterns/atoms/icon_buttons/icon_button.html" with modifier="close" data="data-micromodal-close" aria='aria-label="Close modal"' %}
</div>
</header>
<main class="modal__content" id="filters-content">
<iframe
src="{{ value.get_button_link }}"
width="100%"
height="650px"
frameborder="0"
title="Modal Webform">
</iframe>
</main>
</div>
</div>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
context:
value:
sticky_text: Get in touch
sticky_subtext: learn about our journey
button_link:
- modal_iframe:
url: 'https://example.com'
117 changes: 117 additions & 0 deletions tbx/static_src/javascript/components/modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import MicroModal from 'micromodal'; // es6 module

// Assumes a strcuture as follows
// <div class="modal" id="filters" aria-hidden="true">
// <div class="modal__overlay" tabindex="-1" data-micromodal-close></div>
// <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-title" >
// <header class="modal__header">
// <h2 class="modal__heading heading heading--two" id="modal-title">Title<h2>
// <div class="modal__close">
// {% include "atoms/icon_buttons/icon_button.html" with modifier="close" data="data-micromodal-close" aria='aria-label="Close modal"' %}
// </div>
// </header>
// <main class="modal__content" id="filters-content">
// Content
// </main>
// <footer class="modal__footer">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: remove the modal footer as we don't have it in our implementation.
We can probably refer to the markup from dev tools, as currently we have a few more discrepancies, like aria-hidden="true" on modal, Django inclusion tag, etc.

// <button class="modal__btn" data-micromodal-close>Close</button>
// </footer>
// </div>
// </div>

class Modal {
static selector() {
return '[data-micromodal-trigger]';
}

constructor() {
if (typeof MicroModal !== 'undefined') {
MicroModal.init({
openTrigger: 'data-micromodal-trigger',
disableScroll: true,
});
}

Modal.bindEvents();
}

static bindEvents() {
// Listen for clicks on the document instead of using micromodel default, which doesn't work with htmx
document.body.addEventListener('click', Modal.handleEvent);
document.body.addEventListener('touchstart', Modal.handleEvent);
document.body.addEventListener('keydown', Modal.handleKeyDown);
}

static handleEvent(event) {
const trigger = event.target.closest('[data-micromodal-trigger]');
if (trigger) {
event.preventDefault();
event.stopPropagation(); // Stop the event from bubbling up

// Get the modal ID and open the correct modal
const modalId = trigger.getAttribute('data-micromodal-trigger');
if (modalId && typeof MicroModal !== 'undefined') {
MicroModal.show(modalId);
// Ensure tabbing forward from iframes stays within the modal
Modal.ensurePostIframeFocusTrap(modalId);
}
}

// Close modal when clicking on close buttons
const closeButton = event.target.closest(
'[data-micromodal-close], [data-listing-submit]',
);
if (closeButton) {
const modal = closeButton.closest('.modal');
if (modal) {
MicroModal.close(modal.id); // Close the modal
document.body.style.overflow = ''; // Remove overflow hidden from body
}
}
}

// Prevent Enter from closing modal unless on button
static handleKeyDown(event) {
if (event.key === 'Enter') {
const modal = event.target.closest('.modal');
if (modal) {
// Allow Enter on buttons and prevent it on everything else
if (event.target.tagName !== 'BUTTON') {
event.preventDefault();
event.stopPropagation();
}
}
}
}

// When a modal contains an iframe, browser-level tabbing inside the iframe
// does not bubble key events to the parent, so focus-trap libraries
// cannot reliably intercept the Tab press. Add a focus sentinel immediately
// after the iframe that redirects focus to the Close button.
static ensurePostIframeFocusTrap(modalId) {
const modal = document.getElementById(modalId);
if (!modal) return;
const container = modal.querySelector('.modal__container');
if (!container) return;

const iframe = container.querySelector('iframe');
if (!iframe) return;

// Only add once per modal instance
if (container.querySelector('.modal__focus-sentinel')) return;

const sentinel = document.createElement('span');
sentinel.tabIndex = 0;

sentinel.addEventListener('focus', () => {
const closeButton = modal.querySelector('[data-micromodal-close]');
if (closeButton) {
closeButton.focus();
}
});

iframe.parentNode.insertBefore(sentinel, iframe.nextSibling);
}
}

export default Modal;
10 changes: 10 additions & 0 deletions tbx/static_src/javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CookieWarning from './components/cookie-message';
import YouTubeConsentManager from './components/youtube-embed';
import Tabs from './components/tabs';
import TableHint from './components/table-hint';
import Modal from './components/modal';
import ModeSwitcher from './components/mode-switcher';

// IE11 polyfills
Expand Down Expand Up @@ -38,6 +39,15 @@ document.addEventListener('DOMContentLoaded', () => {
initComponent(YouTubeConsentManager);
initComponent(Tabs);
initComponent(TableHint);
initComponent(Modal);
initComponent(ModeSwitcher);
new DesktopCloseMenus();

// Move sticky CTA(s) to the end of the main content for natural tab order
const main = document.getElementById('main-content') || document.body;
if (main) {
document.querySelectorAll('[data-sticky-cta]').forEach((element) => {
main.appendChild(element);
});
}
});
Loading