Skip to content
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
2 changes: 1 addition & 1 deletion dist/cookieconsent.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cookieconsent.umd.js

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,18 @@ export const show = (createModal) => {
if (_state._disablePageInteraction)
toggleDisableInteraction(true);

focusAfterTransition(_dom._cm, 1);

addClass(_dom._htmlDom, TOGGLE_CONSENT_MODAL_CLASS);
setAttribute(_dom._cm, ARIA_HIDDEN, 'false');

/**
* Set focus to consentModal and enable transition
*/
setTimeout(() => {
addClass(_dom._cmContainer, 'cc--anim');
focus(globalObj._dom._cmDivTabindex);
addClass(globalObj._dom._cmContainer, 'cc--anim');
}, 100);
setTimeout(() => {
focus(globalObj._dom._cmDivTabindex);
}, 200);

debug('CookieConsent [TOGGLE]: show consentModal');

Expand Down Expand Up @@ -245,7 +245,7 @@ export const hide = () => {
*/
focus(_state._lastFocusedElemBeforeModal);
_state._lastFocusedElemBeforeModal = null;

/**
* Remove transition class after it executed
*/
Expand Down Expand Up @@ -277,8 +277,6 @@ export const showPreferences = () => {
state._lastFocusedModalElement = getActiveElement();
}

focusAfterTransition(globalObj._dom._pm, 2);

addClass(globalObj._dom._htmlDom, TOGGLE_PREFERENCES_MODAL_CLASS);
setAttribute(globalObj._dom._pm, ARIA_HIDDEN, 'false');

Expand Down
28 changes: 17 additions & 11 deletions src/core/modals/consentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,21 @@ export const createConsentModal = (api, createMainContainer) => {
addClassCm(dom._cmTexts, 'texts');
addClassCm(dom._cmBtns, 'btns');

setAttribute(dom._cm, 'role', 'dialog');
setAttribute(dom._cm, 'aria-modal', 'true');
if (state._disablePageInteraction) {
setAttribute(dom._cm, 'role', 'dialog');
setAttribute(dom._cm, 'aria-modal', 'true');
} else {
setAttribute(dom._cm, 'role', 'region');
}

setAttribute(dom._cm, ARIA_HIDDEN, 'false');
setAttribute(dom._cm, 'aria-describedby', 'cm__desc');

if (consentModalLabelValue)
if (consentModalLabelValue) {
setAttribute(dom._cm, 'aria-label', consentModalLabelValue);
else if (consentModalTitleValue)
} else if (consentModalTitleValue) {
setAttribute(dom._cm, 'aria-labelledby', 'cm__title');

setAttribute(dom._cm, 'aria-describedby', 'cm__desc');
}
const
boxLayout = 'box',
guiOptions = state._userConfig.guiOptions,
Expand Down Expand Up @@ -133,12 +138,11 @@ export const createConsentModal = (api, createMainContainer) => {
if (acceptAllBtnData || acceptNecessaryBtnData || showPreferencesBtnData)
appendChild(dom._cmBody, dom._cmBtns);

dom._cmDivTabindex = createNode(DIV_TAG);
setAttribute(dom._cmDivTabindex, 'tabIndex', -1);
appendChild(dom._cm, dom._cmDivTabindex);

appendChild(dom._cm, dom._cmBody);
appendChild(dom._cmContainer, dom._cm);

dom._cmDivTabindex = dom._cm;
setAttribute(dom._cmDivTabindex, 'tabIndex', -1);
}

if (consentModalTitleValue) {
Expand Down Expand Up @@ -274,7 +278,9 @@ export const createConsentModal = (api, createMainContainer) => {
fireEvent(globalObj._customEvents._onModalReady, CONSENT_MODAL_NAME, dom._cm);
createMainContainer(api);
appendChild(dom._ccMain, dom._cmContainer);
handleFocusTrap(dom._cm);
if (state._disablePageInteraction) {
handleFocusTrap(dom._cm);
}
}

getModalFocusableData(1);
Expand Down
3 changes: 1 addition & 2 deletions src/core/modals/preferencesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ export const createPreferencesModal = (api, createMainContainer) => {
appendChild(dom._pmHeader, dom._pmTitle);
appendChild(dom._pmHeader, dom._pmCloseBtn);

dom._pmDivTabindex = createNode(DIV_TAG);
dom._pmDivTabindex = dom._pmTitle;
setAttribute(dom._pmDivTabindex, 'tabIndex', -1);
appendChild(dom._pm, dom._pmDivTabindex);

appendChild(dom._pm, dom._pmHeader);
appendChild(dom._pm, dom._pmBody);
Expand Down
20 changes: 1 addition & 19 deletions src/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,24 +723,6 @@ export const focus = (el, toggleTabIndex) => {
toggleTabIndex && el.removeAttribute('tabindex');
};

/**
* @param {HTMLDivElement} element
* @param {1 | 2} modalId
*/
export const focusAfterTransition = (element, modalId) => {
const getVisibleDiv = (modalId) => modalId === 1
? globalObj._dom._cmDivTabindex
: globalObj._dom._pmDivTabindex;

const setFocus = (event) => {
event.target.removeEventListener('transitionend', setFocus);
if (event.propertyName === 'opacity' && getComputedStyle(element).opacity === '1') {
focus(getVisibleDiv(modalId));
}
};

addEvent(element, 'transitionend', setFocus);
};

/**
* Obtain accepted and rejected categories
Expand Down Expand Up @@ -792,7 +774,7 @@ const iconStrokes = [
* @param {number} [strokeWidth]
*/
export const getSvgIcon = (iconIndex = 0, strokeWidth = 1.5) => {
return `<svg viewBox="0 0 24 24" stroke-width="${strokeWidth}"><path d="${iconStrokes[iconIndex]}"/></svg>`;
return `<svg aria-hidden="true" viewBox="0 0 24 24" stroke-width="${strokeWidth}"><path d="${iconStrokes[iconIndex]}"/></svg>`;
};

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/gui.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ describe("Test UI options", () => {
api.show();
await new Promise(r => setTimeout(r, 300));
const currActiveElement = getActiveElement();
const expectedElement = document.querySelector('.cm[tabindex="-1"]');
expect(currActiveElement).not.toBe(prevActiveElement);
expect(document.querySelector('.cm > div[tabIndex="-1"]')).toBe(currActiveElement);
expect(currActiveElement).toBe(expectedElement);
});

it('preferencesModal should receive focus when it is shown', async () => {
Expand All @@ -149,8 +150,10 @@ describe("Test UI options", () => {
api.showPreferences();
await new Promise(r => setTimeout(r, 300));
const currActiveElement = getActiveElement();
const expectedElement = document.querySelector('.pm__title[tabindex="-1"]');
expect(currActiveElement).not.toBe(prevActiveElement);
expect(document.querySelector('.pm > div[tabIndex="-1"]')).toBe(currActiveElement);
expect(currActiveElement).toBe(expectedElement);

});
})

Expand Down