{"version":3,"file":"use-responsive-value.mjs","names":[],"sources":["../../src/utils/use-responsive-value.ts"],"sourcesContent":["import { useEffect, useState } from '@wordpress/element';\n\nconst breakpoints = [ '40em', '52em', '64em' ];\n\nexport const useBreakpointIndex = (\n\toptions: { defaultIndex?: number; enabled?: boolean } = {}\n) => {\n\tconst { defaultIndex = 0, enabled = true } = options;\n\n\tif ( typeof defaultIndex !== 'number' ) {\n\t\tthrow new TypeError(\n\t\t\t`Default breakpoint index should be a number. Got: ${ defaultIndex }, ${ typeof defaultIndex }`\n\t\t);\n\t} else if ( defaultIndex < 0 || defaultIndex > breakpoints.length - 1 ) {\n\t\tthrow new RangeError(\n\t\t\t`Default breakpoint index out of range. Theme has ${ breakpoints.length } breakpoints, got index ${ defaultIndex }`\n\t\t);\n\t}\n\n\tconst [ value, setValue ] = useState( defaultIndex );\n\n\tuseEffect( () => {\n\t\tif ( ! enabled ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst getIndex = () =>\n\t\t\tbreakpoints.filter( ( bp ) => {\n\t\t\t\treturn typeof window !== 'undefined'\n\t\t\t\t\t? window.matchMedia( `screen and (min-width: ${ bp })` )\n\t\t\t\t\t\t\t.matches\n\t\t\t\t\t: false;\n\t\t\t} ).length;\n\n\t\tconst onResize = () => {\n\t\t\tconst newValue = getIndex();\n\t\t\tif ( value !== newValue ) {\n\t\t\t\tsetValue( newValue );\n\t\t\t}\n\t\t};\n\n\t\tonResize();\n\n\t\tif ( typeof window !== 'undefined' ) {\n\t\t\twindow.addEventListener( 'resize', onResize );\n\t\t}\n\t\treturn () => {\n\t\t\tif ( typeof window !== 'undefined' ) {\n\t\t\t\twindow.removeEventListener( 'resize', onResize );\n\t\t\t}\n\t\t};\n\t}, [ value, enabled ] );\n\n\treturn value;\n};\n\nexport function useResponsiveValue< T >(\n\tvalues: ( T | undefined )[],\n\toptions: Parameters< typeof useBreakpointIndex >[ 0 ] = {}\n): T | undefined {\n\t// A single value is the same at every breakpoint, so there is nothing to\n\t// watch. Most callers pass one, and watching for them would mean a media\n\t// query and a resize listener per component instance.\n\tconst isResponsive = Array.isArray( values ) && values.length > 1;\n\tconst index = useBreakpointIndex( { ...options, enabled: isResponsive } );\n\n\t// Allow calling the function with a \"normal\" value without having to check on the outside.\n\tif ( ! Array.isArray( values ) && typeof values !== 'function' ) {\n\t\treturn values;\n\t}\n\n\tconst array = values || [];\n\n\treturn array[ index >= array.length ? array.length - 1 : index ];\n}\n"],"mappings":";;AAEA,MAAM,cAAc;CAAE;CAAQ;CAAQ;AAAO;AAE7C,MAAa,sBACZ,UAAwD,CAAC,MACrD;CACJ,MAAM,EAAE,eAAe,GAAG,UAAU,SAAS;CAE7C,IAAK,OAAO,iBAAiB,UAC5B,MAAM,IAAI,UACT,qDAAsD,aAAc,IAAK,OAAO,cACjF;MACM,IAAK,eAAe,KAAK,eAAe,YAAY,SAAS,GACnE,MAAM,IAAI,WACT,oDAAqD,YAAY,OAAQ,0BAA2B,cACrG;CAGD,MAAM,CAAE,OAAO,YAAa,SAAU,YAAa;CAEnD,gBAAiB;EAChB,IAAK,CAAE,SACN;EAGD,MAAM,iBACL,YAAY,QAAU,OAAQ;GAC7B,OAAO,OAAO,WAAW,cACtB,OAAO,WAAY,0BAA2B,GAAI,EAAG,CAAC,CACrD,UACD;EACJ,CAAE,CAAC,CAAC;EAEL,MAAM,iBAAiB;GACtB,MAAM,WAAW,SAAS;GAC1B,IAAK,UAAU,UACd,SAAU,QAAS;EAErB;EAEA,SAAS;EAET,IAAK,OAAO,WAAW,aACtB,OAAO,iBAAkB,UAAU,QAAS;EAE7C,aAAa;GACZ,IAAK,OAAO,WAAW,aACtB,OAAO,oBAAqB,UAAU,QAAS;EAEjD;CACD,GAAG,CAAE,OAAO,OAAQ,CAAE;CAEtB,OAAO;AACR;AAEA,SAAgB,mBACf,QACA,UAAwD,CAAC,GACzC;CAIhB,MAAM,eAAe,MAAM,QAAS,MAAO,KAAK,OAAO,SAAS;CAChE,MAAM,QAAQ,mBAAoB;EAAE,GAAG;EAAS,SAAS;CAAa,CAAE;CAGxE,IAAK,CAAE,MAAM,QAAS,MAAO,KAAK,OAAO,WAAW,YACnD,OAAO;CAGR,MAAM,QAAQ,UAAU,CAAC;CAEzB,OAAO,MAAO,SAAS,MAAM,SAAS,MAAM,SAAS,IAAI;AAC1D"}