|
| 1 | +import * as React from "react"; |
| 2 | +import {StyleSheet} from "aphrodite"; |
| 3 | +import type {Meta, StoryObj} from "@storybook/react"; |
| 4 | + |
| 5 | +import { |
| 6 | + announceMessage, |
| 7 | + type AnnounceMessageProps, |
| 8 | +} from "@khanacademy/wonder-blocks-announcer"; |
| 9 | +import Button from "@khanacademy/wonder-blocks-button"; |
| 10 | +import {View} from "@khanacademy/wonder-blocks-core"; |
| 11 | + |
| 12 | +import ComponentInfo from "../components/component-info"; |
| 13 | +import packageConfig from "../../packages/wonder-blocks-announcer/package.json"; |
| 14 | + |
| 15 | +const AnnouncerExample = ({ |
| 16 | + message = "Clicked!", |
| 17 | + level, |
| 18 | + debounceThreshold, |
| 19 | +}: AnnounceMessageProps) => { |
| 20 | + return ( |
| 21 | + <Button |
| 22 | + onClick={async () => { |
| 23 | + const idRef = await announceMessage({ |
| 24 | + message, |
| 25 | + level, |
| 26 | + debounceThreshold, |
| 27 | + }); |
| 28 | + /* eslint-disable-next-line */ |
| 29 | + console.log(idRef); |
| 30 | + }} |
| 31 | + > |
| 32 | + Save |
| 33 | + </Button> |
| 34 | + ); |
| 35 | +}; |
| 36 | +type StoryComponentType = StoryObj<typeof AnnouncerExample>; |
| 37 | + |
| 38 | +/** |
| 39 | + * Announcer exposes an API for screen reader messages using ARIA Live Regions. |
| 40 | + * It can be used to notify Assistive Technology users without moving focus. Use |
| 41 | + * cases include combobox filtering, toast notifications, client-side routing, |
| 42 | + * and more. |
| 43 | + * |
| 44 | + * Calling the `announceMessage` function automatically appends the appropriate live regions |
| 45 | + * to the document body. It sends messages at a default `polite` level, with the |
| 46 | + * ability to override to `assertive` by passing a `level` argument. You can also |
| 47 | + * pass a `debounceThreshold` to wait a specific duration before making another announcement. |
| 48 | + * |
| 49 | + * To test this API, turn on VoiceOver for Mac/iOS or NVDA on Windows and click the example button. |
| 50 | + * |
| 51 | + * ### Usage |
| 52 | + * ```jsx |
| 53 | + * import { appendMessage } from "@khanacademy/wonder-blocks-announcer"; |
| 54 | + * |
| 55 | + * <div> |
| 56 | + * <button onClick={() => appendMessage({message: 'Saved your work for you.'})}> |
| 57 | + * Save |
| 58 | + * </button> |
| 59 | + * </div> |
| 60 | + * ``` |
| 61 | + */ |
| 62 | +export default { |
| 63 | + title: "Packages / Announcer", |
| 64 | + component: AnnouncerExample, |
| 65 | + decorators: [ |
| 66 | + (Story): React.ReactElement<React.ComponentProps<typeof View>> => ( |
| 67 | + <View style={styles.example}> |
| 68 | + <Story /> |
| 69 | + </View> |
| 70 | + ), |
| 71 | + ], |
| 72 | + parameters: { |
| 73 | + addBodyClass: "showAnnouncer", |
| 74 | + componentSubtitle: ( |
| 75 | + <ComponentInfo |
| 76 | + name={packageConfig.name} |
| 77 | + version={packageConfig.version} |
| 78 | + /> |
| 79 | + ), |
| 80 | + docs: { |
| 81 | + source: { |
| 82 | + // See https://github.com/storybookjs/storybook/issues/12596 |
| 83 | + excludeDecorators: true, |
| 84 | + }, |
| 85 | + }, |
| 86 | + chromatic: {disableSnapshot: true}, |
| 87 | + }, |
| 88 | + argTypes: { |
| 89 | + level: { |
| 90 | + control: "radio", |
| 91 | + options: ["polite", "assertive"], |
| 92 | + }, |
| 93 | + debounceThreshold: { |
| 94 | + control: "number", |
| 95 | + type: "number", |
| 96 | + description: "(milliseconds)", |
| 97 | + }, |
| 98 | + }, |
| 99 | +} as Meta<typeof AnnouncerExample>; |
| 100 | + |
| 101 | +/** |
| 102 | + * This is an example of a live region with all the options set to their default |
| 103 | + * values and the `message` argument set to some example text. |
| 104 | + */ |
| 105 | +export const SendMessage: StoryComponentType = { |
| 106 | + args: { |
| 107 | + message: "Here is some example text.", |
| 108 | + level: "polite", |
| 109 | + }, |
| 110 | +}; |
| 111 | + |
| 112 | +const styles = StyleSheet.create({ |
| 113 | + example: { |
| 114 | + alignItems: "center", |
| 115 | + justifyContent: "center", |
| 116 | + }, |
| 117 | +}); |
0 commit comments