Skip to content
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

Docs: Add an example page #4

Closed
wants to merge 3 commits into from
Closed
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
76 changes: 76 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>live-region-element</title>
</head>
<body>
<main>
<h1>live-region-element</h1>
<h2>announce</h2>

<h3>Polite</h3>
<button id="js-announce-polite">Announce politely</button>

<h3>Assertive</h3>
<button id="js-announce-assertive">Announce assertively</button>

<h2>announceFromElement</h2>
<h3>Polite</h3>
<p id="some-text">Hello world.</p>
<button id="js-announce-el-polite">Announce element politely</button>

<h3>Assertive</h3>
<p id="more-text">Goodbye for now.</p>
<button id="js-announce-el-assertive">Announce element assertively</button>

<live-region>
<template shadowrootmode="open">
<style>
:host {
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
</style>
<div id="polite" aria-live="polite" aria-atomic="true"></div>
<div id="assertive" aria-live="assertive" aria-atomic="true"></div>
</template>
</live-region>
</main>

<script type="module" src="../dist/esm/index.js"></script>
<script>
document.getElementById('js-announce-polite').addEventListener('click', () => {
const liveRegion = document.querySelector('live-region')
liveRegion.announce('Example polite message', {
politeness: 'polite',
})
})

document.getElementById('js-announce-assertive').addEventListener('click', () => {
const liveRegion = document.querySelector('live-region')
liveRegion.announce('Example assertive message', {
politeness: 'assertive',
})
})

document.getElementById('js-announce-el-polite').addEventListener('click', () => {
const liveRegion = document.querySelector('live-region')
liveRegion.announceFromElement(document.querySelector('#some-text'), {
politeness: 'polite',
})
})

document.getElementById('js-announce-el-assertive').addEventListener('click', () => {
const liveRegion = document.querySelector('live-region')
liveRegion.announceFromElement(document.querySelector('#more-text'), {
politeness: 'assertive',
})
})
</script>

</body>
</html>
11 changes: 11 additions & 0 deletions src/live-region-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,16 @@ function getTemplate() {
return template
}

declare global {
Copy link
Contributor Author

@khiga8 khiga8 Feb 13, 2024

Choose a reason for hiding this comment

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

I wasn't able to access the utility methods on the custom element without this declaration/definition which I noticed our other custom elements (e.g. x_banner, and others) have.

Should this be on the consumer to define when they consume the component?

interface Window {
LiveRegionElement: typeof LiveRegionElement
}
}

if (!window.customElements.get('live-region')) {
window.LiveRegionElement = LiveRegionElement
window.customElements.define('live-region', LiveRegionElement)
}

export {LiveRegionElement, templateContent}
export type {AnnounceOptions}
Loading