tldraw
Version:
A tiny little drawing editor.
8 lines (7 loc) • 2.11 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../../src/lib/ui/context/a11y.tsx"],
"sourcesContent": ["import { Atom, useAtom } from '@tldraw/editor'\nimport { createContext, useContext, useMemo } from 'react'\n\n/** @public */\nexport type A11yPriority = 'polite' | 'assertive'\n\n/** @public */\nexport interface TLUiA11y {\n\tmsg: string | undefined\n\tpriority?: A11yPriority\n}\n\n/** @public */\nexport interface TLUiA11yContextType {\n\tannounce(msg: TLUiA11y): void\n\tcurrentMsg: Atom<TLUiA11y>\n}\n\n/** @internal */\nexport const A11yContext = createContext<TLUiA11yContextType | null>(null)\n\n/** @public */\nexport interface A11yProviderProps {\n\tchildren: React.ReactNode\n}\n\n/** @public @react */\nexport function TldrawUiA11yProvider({ children }: A11yProviderProps) {\n\tconst currentMsg = useAtom<TLUiA11y>('a11y', { msg: '', priority: 'assertive' })\n\tconst ctx = useContext(A11yContext)\n\n\tconst current = useMemo(\n\t\t() => ({\n\t\t\tcurrentMsg,\n\t\t\tannounce(msg: TLUiA11y) {\n\t\t\t\tif (!msg) return\n\t\t\t\tcurrentMsg.set(msg)\n\t\t\t},\n\t\t}),\n\t\t[currentMsg]\n\t)\n\n\t// if the user has already provided a context higher up, reuse that one\n\tif (ctx) {\n\t\treturn <>{children}</>\n\t}\n\n\treturn <A11yContext.Provider value={current}>{children}</A11yContext.Provider>\n}\n\n/** @public */\nexport function useA11y() {\n\tconst ctx = useContext(A11yContext)\n\n\tif (!ctx) {\n\t\tthrow new Error('useA11y must be used within a A11yContext.Provider')\n\t}\n\n\treturn ctx\n}\n"],
"mappings": "AA4CS;AA5CT,SAAe,eAAe;AAC9B,SAAS,eAAe,YAAY,eAAe;AAkB5C,MAAM,cAAc,cAA0C,IAAI;AAQlE,SAAS,qBAAqB,EAAE,SAAS,GAAsB;AACrE,QAAM,aAAa,QAAkB,QAAQ,EAAE,KAAK,IAAI,UAAU,YAAY,CAAC;AAC/E,QAAM,MAAM,WAAW,WAAW;AAElC,QAAM,UAAU;AAAA,IACf,OAAO;AAAA,MACN;AAAA,MACA,SAAS,KAAe;AACvB,YAAI,CAAC,IAAK;AACV,mBAAW,IAAI,GAAG;AAAA,MACnB;AAAA,IACD;AAAA,IACA,CAAC,UAAU;AAAA,EACZ;AAGA,MAAI,KAAK;AACR,WAAO,gCAAG,UAAS;AAAA,EACpB;AAEA,SAAO,oBAAC,YAAY,UAAZ,EAAqB,OAAO,SAAU,UAAS;AACxD;AAGO,SAAS,UAAU;AACzB,QAAM,MAAM,WAAW,WAAW;AAElC,MAAI,CAAC,KAAK;AACT,UAAM,IAAI,MAAM,oDAAoD;AAAA,EACrE;AAEA,SAAO;AACR;",
"names": []
}