Skip to content

Commit

Permalink
Merge pull request #529 from EreMaijala/v3.0-beta-html-fixes
Browse files Browse the repository at this point in the history
Clean up extraneous attributes to pass HTML validation.
  • Loading branch information
orestbida authored Jun 12, 2023
2 parents c5a64e2 + a0b9aef commit a21b4e5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
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.

8 changes: 4 additions & 4 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ export const hide = () => {
toggleDisableInteraction();

/**
* Fix focus restoration
* Fix focus restoration to body with Chrome
*/
focus(_dom._focusSpan);
focus(_dom._focusSpan, false, true);

removeClass(_dom._htmlDom, TOGGLE_CONSENT_MODAL_CLASS);
setAttribute(_dom._cm, ARIA_HIDDEN, 'true');
Expand Down Expand Up @@ -327,9 +327,9 @@ export const hidePreferences = () => {
discardUnsavedPreferences();

/**
* Fix focus restoration
* Fix focus restoration to body with Chrome
*/
focus(globalObj._dom._pmFocusSpan);
focus(globalObj._dom._pmFocusSpan, false, true);

removeClass(globalObj._dom._htmlDom, TOGGLE_PREFERENCES_MODAL_CLASS);
setAttribute(globalObj._dom._pm, ARIA_HIDDEN, 'true');
Expand Down
1 change: 0 additions & 1 deletion src/core/modals/consentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { createPreferencesModal } from './preferencesModal';
*/
const createFocusSpan = () => {
const span = createNode('span');
span.tabIndex = -1;

if(!globalObj._dom._focusSpan)
globalObj._dom._focusSpan = span;
Expand Down
3 changes: 0 additions & 3 deletions src/core/modals/preferencesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export const createPreferencesModal = (api, createMainContainer) => {
addEvent(dom._pmCloseBtn, CLICK_EVENT, hidePreferences);

dom._pmFocusSpan = createNode('span');
dom._pmFocusSpan.tabIndex = -1;
dom._pmFocusSpan.innerHTML = getSvgIcon();
appendChild(dom._pmCloseBtn, dom._pmFocusSpan);

Expand Down Expand Up @@ -360,7 +359,6 @@ export const createPreferencesModal = (api, createMainContainer) => {
*/
const trHeadFragment = dom._document.createDocumentFragment();
const trHead = createNode('tr');
setAttribute(trHead, 'role', 'row');

for(const headerKey of tableHeadersKeys){
const headerValue = headerData[headerKey];
Expand All @@ -385,7 +383,6 @@ export const createPreferencesModal = (api, createMainContainer) => {
for(const bodyItem of sCookieTableBody){

const tr = createNode('tr');
setAttribute(tr, 'role', 'row');
addClassPm(tr, 'table-tr');

for(const tdKey of tableHeadersKeys){
Expand Down
20 changes: 18 additions & 2 deletions src/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,19 @@ export const addDataButtonListeners = (elem, api, createPreferencesModal, create
/**
* @param {HTMLElement} el
* @param {1 | 2} [modalId]
* @param {boolean} [toggleTabIndex]
*/
export const focus = (el, modalId) => {
el && el.focus();
export const focus = (el, modalId, toggleTabIndex) => {

if(el){
/**
* Momentarily add the `tabindex` attribute to fix
* a bug with focus restoration in chrome
*/
toggleTabIndex && (el.tabIndex = -1);

el.focus();
}

if(modalId) {
globalObj._state._currentFocusedModal = modalId === 1
Expand All @@ -738,6 +748,12 @@ export const focus = (el, modalId) => {
? globalObj._state._cmFocusableElements
: globalObj._state._pmFocusableElements;
}

/**
* Remove the `tabindex` attribute so
* that the html markup is valid again
*/
toggleTabIndex && (el && el.removeAttribute('tabindex'));
};

/**
Expand Down

0 comments on commit a21b4e5

Please sign in to comment.