UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

1 lines 3.18 kB
{"version":3,"file":"use-orientation.cjs","names":[],"sources":["../../src/use-orientation/use-orientation.ts"],"sourcesContent":["import { useState } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\n\nexport interface UseOrientationOptions {\n /** Default angle value, used until the real can be retrieved\n * (during server side rendering and before js executes on the page)\n * If not provided, the default value is `0`\n * */\n defaultAngle?: number;\n\n /** Default angle value, used until the real can be retrieved\n * (during server side rendering and before js executes on the page)\n * If not provided, the default value is `'landscape-primary'`\n * */\n defaultType?: OrientationType;\n\n /** If true, the initial value will be resolved in useEffect (ssr safe)\n * If false, the initial value will be resolved in useLayoutEffect (ssr unsafe)\n * True by default.\n */\n getInitialValueInEffect?: boolean;\n}\n\nexport interface UseOrientationReturnType {\n angle: number;\n type: OrientationType;\n}\n\nfunction getInitialValue(\n initialValue: UseOrientationReturnType,\n getInitialValueInEffect: boolean\n): UseOrientationReturnType {\n if (getInitialValueInEffect) {\n return initialValue;\n }\n\n if (typeof window !== 'undefined' && 'screen' in window) {\n return {\n angle: window.screen.orientation?.angle ?? initialValue.angle,\n type: window.screen.orientation?.type ?? initialValue.type,\n };\n }\n\n return initialValue;\n}\n\nexport function useOrientation({\n defaultAngle = 0,\n defaultType = 'landscape-primary',\n getInitialValueInEffect = true,\n}: UseOrientationOptions = {}): UseOrientationReturnType {\n const [orientation, setOrientation] = useState<UseOrientationReturnType>(\n getInitialValue(\n {\n angle: defaultAngle,\n type: defaultType,\n },\n getInitialValueInEffect\n )\n );\n\n const handleOrientationChange = (event: Event) => {\n const target = event.currentTarget as ScreenOrientation;\n setOrientation({ angle: target?.angle || 0, type: target?.type || 'landscape-primary' });\n };\n\n useIsomorphicEffect(() => {\n window.screen.orientation?.addEventListener('change', handleOrientationChange);\n return () => window.screen.orientation?.removeEventListener('change', handleOrientationChange);\n }, []);\n\n return orientation;\n}\n\nexport namespace useOrientation {\n export type Options = UseOrientationOptions;\n export type ReturnType = UseOrientationReturnType;\n}\n"],"mappings":";;;;AA4BA,SAAS,gBACP,cACA,yBAC0B;AAC1B,KAAI,wBACF,QAAO;AAGT,KAAI,OAAO,WAAW,eAAe,YAAY,OAC/C,QAAO;EACL,OAAO,OAAO,OAAO,aAAa,SAAS,aAAa;EACxD,MAAM,OAAO,OAAO,aAAa,QAAQ,aAAa;EACvD;AAGH,QAAO;;AAGT,SAAgB,eAAe,EAC7B,eAAe,GACf,cAAc,qBACd,0BAA0B,SACD,EAAE,EAA4B;CACvD,MAAM,CAAC,aAAa,mBAAA,GAAA,MAAA,UAClB,gBACE;EACE,OAAO;EACP,MAAM;EACP,EACD,wBACD,CACF;CAED,MAAM,2BAA2B,UAAiB;EAChD,MAAM,SAAS,MAAM;AACrB,iBAAe;GAAE,OAAO,QAAQ,SAAS;GAAG,MAAM,QAAQ,QAAQ;GAAqB,CAAC;;AAG1F,+BAAA,0BAA0B;AACxB,SAAO,OAAO,aAAa,iBAAiB,UAAU,wBAAwB;AAC9E,eAAa,OAAO,OAAO,aAAa,oBAAoB,UAAU,wBAAwB;IAC7F,EAAE,CAAC;AAEN,QAAO"}