@wordpress/components
Version:
UI components for WordPress.
1 lines • 3.28 kB
Source Map (JSON)
{"version":3,"file":"aria-helper.cjs","names":[],"sources":["../../src/modal/aria-helper.ts"],"sourcesContent":["const LIVE_REGION_ARIA_ROLES = new Set( [\n\t'alert',\n\t'status',\n\t'log',\n\t'marquee',\n\t'timer',\n] );\n\nconst modalElements: Element[] = [];\nconst hiddenElements = new Set< Element >();\n\n/**\n * Hides all elements in the body element from screen-readers except\n * the provided element and elements that should not be hidden from\n * screen-readers.\n *\n * The reason we do this is because `aria-modal=\"true\"` currently is bugged\n * in Safari, and support is spotty in other browsers overall. In the future\n * we should consider removing these helper functions in favor of\n * `aria-modal=\"true\"`.\n *\n * @param modalElement The element that should not be hidden.\n */\nexport function modalize( modalElement: HTMLDivElement ) {\n\tmodalElements.push( modalElement );\n\t// A modal opened in the same commit may have hidden this one.\n\tif ( hiddenElements.delete( modalElement ) ) {\n\t\tmodalElement.removeAttribute( 'aria-hidden' );\n\t}\n\tfor ( const element of Array.from( document.body.children ) ) {\n\t\tif ( element !== modalElement && elementShouldBeHidden( element ) ) {\n\t\t\telement.setAttribute( 'aria-hidden', 'true' );\n\t\t\thiddenElements.add( element );\n\t\t}\n\t}\n}\n\n/**\n * Determines if the passed element should not be hidden from screen readers.\n *\n * @param element The element that should be checked.\n *\n * @return Whether the element should not be hidden from screen-readers.\n */\nexport function elementShouldBeHidden( element: Element ) {\n\tconst role = element.getAttribute( 'role' );\n\treturn ! (\n\t\telement.tagName === 'SCRIPT' ||\n\t\telement.hasAttribute( 'hidden' ) ||\n\t\telement.hasAttribute( 'aria-hidden' ) ||\n\t\telement.hasAttribute( 'aria-live' ) ||\n\t\t( role && LIVE_REGION_ARIA_ROLES.has( role ) )\n\t);\n}\n\n/**\n * Accessibly reveals the topmost remaining modal, or all hidden elements once\n * the last modal closes.\n *\n * @param modalElement The element of the closing modal.\n */\nexport function unmodalize( modalElement: HTMLDivElement ) {\n\tmodalElements.splice( modalElements.indexOf( modalElement ), 1 );\n\tconst topModalElement = modalElements[ modalElements.length - 1 ];\n\tfor ( const element of hiddenElements ) {\n\t\tif ( ! topModalElement || element === topModalElement ) {\n\t\t\telement.removeAttribute( 'aria-hidden' );\n\t\t\thiddenElements.delete( element );\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;AAAA,MAAM,yCAAyB,IAAI,IAAK;CACvC;CACA;CACA;CACA;CACA;AACD,CAAE;AAEF,MAAM,gBAA2B,CAAC;AAClC,MAAM,iCAAiB,IAAI,IAAe;;;;;;;;;;;;;AAc1C,SAAgB,SAAU,cAA+B;CACxD,cAAc,KAAM,YAAa;CAEjC,IAAK,eAAe,OAAQ,YAAa,GACxC,aAAa,gBAAiB,aAAc;CAE7C,KAAM,MAAM,WAAW,MAAM,KAAM,SAAS,KAAK,QAAS,GACzD,IAAK,YAAY,gBAAgB,sBAAuB,OAAQ,GAAI;EACnE,QAAQ,aAAc,eAAe,MAAO;EAC5C,eAAe,IAAK,OAAQ;CAC7B;AAEF;;;;;;;;AASA,SAAgB,sBAAuB,SAAmB;CACzD,MAAM,OAAO,QAAQ,aAAc,MAAO;CAC1C,OAAO,EACN,QAAQ,YAAY,YACpB,QAAQ,aAAc,QAAS,KAC/B,QAAQ,aAAc,aAAc,KACpC,QAAQ,aAAc,WAAY,KAChC,QAAQ,uBAAuB,IAAK,IAAK;AAE7C;;;;;;;AAQA,SAAgB,WAAY,cAA+B;CAC1D,cAAc,OAAQ,cAAc,QAAS,YAAa,GAAG,CAAE;CAC/D,MAAM,kBAAkB,cAAe,cAAc,SAAS;CAC9D,KAAM,MAAM,WAAW,gBACtB,IAAK,CAAE,mBAAmB,YAAY,iBAAkB;EACvD,QAAQ,gBAAiB,aAAc;EACvC,eAAe,OAAQ,OAAQ;CAChC;AAEF"}