-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: export AnnounceProvider & useAnnounce() #30328
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
Merged
layershifter
merged 2 commits into
microsoft:master
from
layershifter:feat/export-useannounce
Jan 17, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-components-ab114c24-9a9d-42e2-8834-4d2af7fba463.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: export AnnounceProvider & useAnnounce()", | ||
| "packageName": "@fluentui/react-components", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-shared-contexts-9fc4180f-4a72-41c5-91aa-49564ac7d936.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: export AnnounceProvider & useAnnounce()", | ||
| "packageName": "@fluentui/react-shared-contexts", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/react-components/react-shared-contexts/.storybook/main.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const rootMain = require('../../../../.storybook/main'); | ||
|
|
||
| module.exports = /** @type {Omit<import('../../../../.storybook/main'), 'typescript'|'babel'>} */ ({ | ||
| ...rootMain, | ||
| stories: [...rootMain.stories, '../stories/**/*.stories.mdx', '../stories/**/index.stories.@(ts|tsx)'], | ||
| addons: [...rootMain.addons], | ||
| webpackFinal: (config, options) => { | ||
| const localConfig = { ...rootMain.webpackFinal(config, options) }; | ||
|
|
||
| // add your own webpack tweaks if needed | ||
|
|
||
| return localConfig; | ||
| }, | ||
| }); |
7 changes: 7 additions & 0 deletions
7
packages/react-components/react-shared-contexts/.storybook/preview.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import * as rootPreview from '../../../../.storybook/preview'; | ||
|
|
||
| /** @type {typeof rootPreview.decorators} */ | ||
| export const decorators = [...rootPreview.decorators]; | ||
|
|
||
| /** @type {typeof rootPreview.parameters} */ | ||
| export const parameters = { ...rootPreview.parameters }; |
10 changes: 10 additions & 0 deletions
10
packages/react-components/react-shared-contexts/.storybook/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "", | ||
| "allowJs": true, | ||
| "checkJs": true, | ||
| "types": ["static-assets", "environment", "storybook__addons"] | ||
| }, | ||
| "include": ["../stories/**/*.stories.ts", "../stories/**/*.stories.tsx", "*.js"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../react-components/react-shared-contexts/stories/UseAnnouce/UseAnnounceDefault.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import * as React from 'react'; | ||
| import { | ||
| AnnounceProvider, | ||
| Button, | ||
| Divider, | ||
| Field, | ||
| Input, | ||
| Radio, | ||
| RadioGroup, | ||
| useAnnounce, | ||
| } from '@fluentui/react-components'; | ||
| import type { AnnounceContextValue } from '@fluentui/react-components'; | ||
|
|
||
| const AnnouncePlayground: React.FC = () => { | ||
| const { announce } = useAnnounce(); | ||
|
|
||
| const [message, setMessage] = React.useState('Hello world'); | ||
| const [messageType, setMessageType] = React.useState<'polite' | 'assertive'>('polite'); | ||
|
|
||
| return ( | ||
| <> | ||
| <Field label="A message for annoucement"> | ||
| <Input onChange={(ev, data) => setMessage(data.value)} value={message} /> | ||
| </Field> | ||
| <Field label="Message type"> | ||
| <RadioGroup onChange={(ev, data) => setMessageType(data.value as 'polite' | 'assertive')} value={messageType}> | ||
| <Radio label="assertive" value="assertive" /> | ||
| <Radio label="polite" value="polite" /> | ||
| </RadioGroup> | ||
| </Field> | ||
|
|
||
| <Button | ||
| onClick={() => { | ||
| announce(message, { | ||
| polite: messageType === 'polite', | ||
| }); | ||
| }} | ||
| > | ||
| Announce message | ||
| </Button> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export const Default = () => { | ||
| const announce: AnnounceContextValue['announce'] = React.useCallback((message, options) => { | ||
| alert(`Announced {polite: ${String(options?.polite ?? false)}}: ${message}`); | ||
| }, []); | ||
| const value: AnnounceContextValue = React.useMemo(() => ({ announce }), [announce]); | ||
|
|
||
| return ( | ||
| <AnnounceProvider value={value}> | ||
| <p> | ||
| This example shows how to use the <code>useAnnounce()</code> hook, however it does not implement `aria-live` | ||
| regions. | ||
| </p> | ||
|
|
||
| <Divider /> | ||
| <AnnouncePlayground /> | ||
| </AnnounceProvider> | ||
| ); | ||
| }; |
60 changes: 60 additions & 0 deletions
60
...t-components/react-shared-contexts/stories/UseAnnouce/UseAnnounceDescription.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| `useAnnounce()` is a React hook that provides a function that can be used to announce messages to screen readers. | ||
|
|
||
| **Note:** This hook requires an aria-live announcer implementation that is configured through the `<AnnounceProvider />`. Define this context near the top level of your application. | ||
|
|
||
| ## useAnnounce | ||
|
|
||
| `useAnnounce(message, options)` | ||
|
|
||
| - `message` `[string]` is a message to announce | ||
| - `options` is an optional options object | ||
| - `batchId` `[string]` is a unique identifier for the message. If a message with the same id is already announced, it will be replaced. | ||
| - `polite` `[boolean]` indicates that the message announcement can be interrupted by another message and will be announced only user is idle. | ||
| - `priority` `[number]` defines the priority of the message. Higher priority messages will be announced first. | ||
|
|
||
| #### Example | ||
|
|
||
| ```tsx | ||
| import { useAnnounce } from '@fluentui/react-components'; | ||
|
|
||
| function Example() { | ||
| const { announce } = useAnnounce(); | ||
|
|
||
| return <button onClick={() => announce('Hello world!', { polite: true })}>Announce</button>; | ||
| } | ||
| ``` | ||
|
|
||
| ## AnnounceProvider | ||
|
|
||
| `<AnnounceProvider />` is a React component that allows to provide `announce()` function implementation that will be consumed by `useAnnounce()`. | ||
|
|
||
| #### Example | ||
|
|
||
| ```tsx | ||
| import { AnnounceProvider, useAnnounce } from '@fluentui/react-components'; | ||
|
|
||
| function AnnounceConsumer() { | ||
| const { announce } = useAnnounce(); | ||
|
|
||
| // ... | ||
| // component that triggers announcement | ||
| } | ||
|
|
||
| function Announcer(props) { | ||
| const announce = message => { | ||
| // ... | ||
| // implementation of announcement | ||
| }; | ||
| const contextValue = React.useMemo(() => ({ announce }), [announce]); | ||
|
|
||
| return <AnnounceProvider value={contextValue}>{props.children}</AnnounceProvider>; | ||
| } | ||
|
|
||
| function App() { | ||
| return ( | ||
| <AnnounceProvider> | ||
| <AnnounceConsumer /> | ||
| </AnnounceProvider> | ||
| ); | ||
| } | ||
| ``` |
15 changes: 15 additions & 0 deletions
15
packages/react-components/react-shared-contexts/stories/UseAnnouce/index.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import descriptionMd from './UseAnnounceDescription.md'; | ||
|
|
||
| export { Default } from './UseAnnounceDefault.stories'; | ||
|
|
||
| export default { | ||
| title: 'Utilities/ARIA live/useAnnounce', | ||
| component: null, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| component: [descriptionMd].join('\n'), | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ | |
| }, | ||
| { | ||
| "path": "./tsconfig.spec.json" | ||
| }, | ||
| { | ||
| "path": "./.storybook/tsconfig.json" | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.