@wordpress/components
Version:
UI components for WordPress.
1 lines • 3.91 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":["clamp","useControlledState","useCallback"],"sources":["../../src/range-control/utils.ts"],"sourcesContent":["import type { CSSProperties } from 'react';\nimport { useCallback } from '@wordpress/element';\nimport { useControlledState } from '../utils/hooks';\nimport { clamp } from '../utils/math';\nimport type { UseControlledRangeValueArgs } from './types';\n\n/**\n * A float supported clamp function for a specific value.\n *\n * @param value The value to clamp.\n * @param min The minimum value.\n * @param max The maximum value.\n *\n * @return A (float) number\n */\nexport function floatClamp( value: number | null, min: number, max: number ) {\n\tif ( typeof value !== 'number' ) {\n\t\treturn null;\n\t}\n\n\treturn parseFloat( `${ clamp( value, min, max ) }` );\n}\n\n/**\n * Hook to store a clamped value, derived from props.\n *\n * @param settings\n * @return The controlled value and the value setter.\n */\nexport function useControlledRangeValue(\n\tsettings: UseControlledRangeValueArgs\n) {\n\tconst { min, max, value: valueProp, initial } = settings;\n\tconst [ state, setInternalState ] = useControlledState(\n\t\tfloatClamp( valueProp, min, max ),\n\t\t{\n\t\t\tinitial: floatClamp( initial ?? null, min, max ),\n\t\t\tfallback: null,\n\t\t}\n\t);\n\n\tconst setState = useCallback(\n\t\t( nextValue: number | null ) => {\n\t\t\tif ( nextValue === null ) {\n\t\t\t\tsetInternalState( null );\n\t\t\t} else {\n\t\t\t\tsetInternalState( floatClamp( nextValue, min, max ) );\n\t\t\t}\n\t\t},\n\t\t[ min, max, setInternalState ]\n\t);\n\n\t// `state` can't be an empty string because we specified a fallback value of\n\t// `null` in `useControlledState`\n\treturn [ state as Exclude< typeof state, '' >, setState ] as const;\n}\n\nconst CSS_WIDE_KEYWORDS = new Set( [\n\t'inherit',\n\t'initial',\n\t'unset',\n\t'revert',\n\t'revert-layer',\n] );\n\n/**\n * Resolve CSS-wide keyword spellings for the private color classes without\n * changing the original color value passed to the stylesheet.\n *\n * @param value The CSS color value.\n */\nexport function getCSSWideKeyword( value: CSSProperties[ 'color' ] ) {\n\tif ( typeof value !== 'string' ) {\n\t\treturn undefined;\n\t}\n\n\tconst keyword = value\n\t\t.replace(\n\t\t\t/\\\\([a-f\\d]{1,6})[ \\t\\n\\r\\f]?|\\\\([^\\n\\r\\f])|\\/\\*[\\s\\S]*?\\*\\//gi,\n\t\t\t(\n\t\t\t\t_match,\n\t\t\t\thex: string | undefined,\n\t\t\t\tcharacter: string | undefined\n\t\t\t) => {\n\t\t\t\tif ( hex ) {\n\t\t\t\t\tconst codePoint = parseInt( hex, 16 );\n\t\t\t\t\treturn String.fromCodePoint(\n\t\t\t\t\t\tcodePoint > 0 && codePoint <= 0x10ffff\n\t\t\t\t\t\t\t? codePoint\n\t\t\t\t\t\t\t: 0xfffd\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn character ?? ''; // Remove CSS comments before matching keywords.\n\t\t\t}\n\t\t)\n\t\t.replace( /^[ \\t\\n\\r\\f]+|[ \\t\\n\\r\\f]+$/g, '' )\n\t\t.toLowerCase();\n\n\treturn CSS_WIDE_KEYWORDS.has( keyword ) ? keyword : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAeA,SAAgB,WAAY,OAAsB,KAAa,KAAc;CAC5E,IAAK,OAAO,UAAU,UACrB,OAAO;CAGR,OAAO,WAAY,IAAA,GAAIA,iBAAAA,MAAAA,CAAO,OAAO,KAAK,GAAI,GAAK;AACpD;;;;;;;AAQA,SAAgB,wBACf,UACC;CACD,MAAM,EAAE,KAAK,KAAK,OAAO,WAAW,YAAY;CAChD,MAAM,CAAE,OAAO,qBAAA,GAAqBC,wBAAAA,mBAAAA,CACnC,WAAY,WAAW,KAAK,GAAI,GAChC;EACC,SAAS,WAAY,WAAW,MAAM,KAAK,GAAI;EAC/C,UAAU;CACX,CACD;CAeA,OAAO,CAAE,QAAA,GAbQC,mBAAAA,YAAAA,EACd,cAA8B;EAC/B,IAAK,cAAc,MAClB,iBAAkB,IAAK;OAEvB,iBAAkB,WAAY,WAAW,KAAK,GAAI,CAAE;CAEtD,GACA;EAAE;EAAK;EAAK;CAAiB,CAKwB,CAAE;AACzD;AAEA,MAAM,oCAAoB,IAAI,IAAK;CAClC;CACA;CACA;CACA;CACA;AACD,CAAE;;;;;;;AAQF,SAAgB,kBAAmB,OAAkC;CACpE,IAAK,OAAO,UAAU,UACrB;CAGD,MAAM,UAAU,MACd,QACA,kEAEC,QACA,KACA,cACI;EACJ,IAAK,KAAM;GACV,MAAM,YAAY,SAAU,KAAK,EAAG;GACpC,OAAO,OAAO,cACb,YAAY,KAAK,aAAa,UAC3B,YACA,KACJ;EACD;EACA,OAAO,aAAa;CACrB,CACD,CAAC,CACA,QAAS,gCAAgC,EAAG,CAAC,CAC7C,YAAY;CAEd,OAAO,kBAAkB,IAAK,OAAQ,IAAI,UAAU,KAAA;AACrD"}