UNPKG

tldraw

Version:

A tiny little drawing editor.

8 lines (7 loc) 2.31 kB
{ "version": 3, "sources": ["../../../../src/lib/ui/context/breakpoints.tsx"], "sourcesContent": ["import { useMaybeEditor, useValue } from '@tldraw/editor'\nimport React, { ReactNode, useContext } from 'react'\nimport { PORTRAIT_BREAKPOINT, PORTRAIT_BREAKPOINTS } from '../constants'\n\nconst BreakpointContext = React.createContext<number | null>(null)\n\n/** @public */\nexport interface BreakPointProviderProps {\n\tforceMobile?: boolean\n\tchildren: ReactNode\n}\n\n/** @public @react */\nexport function BreakPointProvider({ forceMobile = false, children }: BreakPointProviderProps) {\n\tconst editor = useMaybeEditor()\n\n\tconst breakpoint = useValue(\n\t\t'breakpoint',\n\t\t() => {\n\t\t\t// This will recompute the viewport screen bounds changes...\n\t\t\tconst { width } = editor?.getViewportScreenBounds() ?? { width: window.innerWidth }\n\n\t\t\tconst maxBreakpoint = forceMobile\n\t\t\t\t? PORTRAIT_BREAKPOINT.MOBILE_SM\n\t\t\t\t: PORTRAIT_BREAKPOINTS.length - 1\n\n\t\t\tfor (let i = 0; i < maxBreakpoint; i++) {\n\t\t\t\tif (width > PORTRAIT_BREAKPOINTS[i] && width <= PORTRAIT_BREAKPOINTS[i + 1]) {\n\t\t\t\t\treturn i\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn maxBreakpoint\n\t\t},\n\t\t[editor]\n\t)\n\n\treturn <BreakpointContext.Provider value={breakpoint}>{children}</BreakpointContext.Provider>\n}\n\n/** @public */\nexport function useBreakpoint() {\n\tconst breakpoint = useContext(BreakpointContext)\n\tif (breakpoint === null) {\n\t\tthrow new Error('useBreakpoint must be used inside of the <BreakpointProvider /> component')\n\t}\n\treturn breakpoint\n}\n"], "mappings": "AAqCQ;AArCR,SAAS,gBAAgB,gBAAgB;AACzC,OAAO,SAAoB,kBAAkB;AAC7C,SAAS,qBAAqB,4BAA4B;AAE1D,MAAM,oBAAoB,MAAM,cAA6B,IAAI;AAS1D,SAAS,mBAAmB,EAAE,cAAc,OAAO,SAAS,GAA4B;AAC9F,QAAM,SAAS,eAAe;AAE9B,QAAM,aAAa;AAAA,IAClB;AAAA,IACA,MAAM;AAEL,YAAM,EAAE,MAAM,IAAI,QAAQ,wBAAwB,KAAK,EAAE,OAAO,OAAO,WAAW;AAElF,YAAM,gBAAgB,cACnB,oBAAoB,YACpB,qBAAqB,SAAS;AAEjC,eAAS,IAAI,GAAG,IAAI,eAAe,KAAK;AACvC,YAAI,QAAQ,qBAAqB,CAAC,KAAK,SAAS,qBAAqB,IAAI,CAAC,GAAG;AAC5E,iBAAO;AAAA,QACR;AAAA,MACD;AAEA,aAAO;AAAA,IACR;AAAA,IACA,CAAC,MAAM;AAAA,EACR;AAEA,SAAO,oBAAC,kBAAkB,UAAlB,EAA2B,OAAO,YAAa,UAAS;AACjE;AAGO,SAAS,gBAAgB;AAC/B,QAAM,aAAa,WAAW,iBAAiB;AAC/C,MAAI,eAAe,MAAM;AACxB,UAAM,IAAI,MAAM,2EAA2E;AAAA,EAC5F;AACA,SAAO;AACR;", "names": [] }