@wordpress/components
Version:
UI components for WordPress.
1 lines • 11.4 kB
Source Map (JSON)
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/border-box-control/utils.ts"],"sourcesContent":["import type { CSSProperties } from 'react';\nimport { parseCSSUnitValue } from '../utils/unit-values';\nimport type { Border } from '../border-control/types';\nimport type { AnyBorder, Borders, BorderProp, BorderSide } from './types';\n\nconst sides: BorderSide[] = [ 'top', 'right', 'bottom', 'left' ];\nconst borderProps: BorderProp[] = [ 'color', 'style', 'width' ];\n\nexport const isEmptyBorder = ( border?: Border ) => {\n\tif ( ! border ) {\n\t\treturn true;\n\t}\n\treturn ! borderProps.some( ( prop ) => border[ prop ] !== undefined );\n};\n\nexport const isDefinedBorder = ( border: AnyBorder ) => {\n\t// No border, no worries :)\n\tif ( ! border ) {\n\t\treturn false;\n\t}\n\n\t// If we have individual borders per side within the border object we\n\t// need to check whether any of those side borders have been set.\n\tif ( hasSplitBorders( border ) ) {\n\t\tconst allSidesEmpty = sides.every( ( side ) =>\n\t\t\tisEmptyBorder( ( border as Borders )[ side ] )\n\t\t);\n\n\t\treturn ! allSidesEmpty;\n\t}\n\n\t// If we have a top-level border only, check if that is empty. e.g.\n\t// { color: undefined, style: undefined, width: undefined }\n\t// Border radius can still be set within the border object as it is\n\t// handled separately.\n\treturn ! isEmptyBorder( border as Border );\n};\n\nexport const isCompleteBorder = ( border?: Border ) => {\n\tif ( ! border ) {\n\t\treturn false;\n\t}\n\n\treturn borderProps.every( ( prop ) => border[ prop ] !== undefined );\n};\n\nexport const hasSplitBorders = ( border: AnyBorder = {} ) => {\n\treturn Object.keys( border ).some(\n\t\t( side ) => sides.indexOf( side as BorderSide ) !== -1\n\t);\n};\n\nexport const hasMixedBorders = ( borders: AnyBorder ) => {\n\tif ( ! hasSplitBorders( borders ) ) {\n\t\treturn false;\n\t}\n\n\tconst shorthandBorders = sides.map( ( side: BorderSide ) =>\n\t\tgetShorthandBorderStyle( ( borders as Borders )?.[ side ] )\n\t);\n\n\treturn ! shorthandBorders.every(\n\t\t( border ) => border === shorthandBorders[ 0 ]\n\t);\n};\n\n/**\n * Splits a CSS box shorthand string into parts, preserving values\n * with parentheses like `calc()` and `var()`.\n *\n * @param value The shorthand string to split.\n *\n * @return Array of split values.\n */\nexport function splitBoxShorthand( value: string ): string[] {\n\tconst parts: string[] = [];\n\tlet current = '';\n\tlet depth = 0;\n\n\tfor ( const char of value.trim() ) {\n\t\tif ( char === '(' ) {\n\t\t\tdepth++;\n\t\t\tcurrent += char;\n\t\t} else if ( char === ')' ) {\n\t\t\tdepth = Math.max( 0, depth - 1 );\n\t\t\tcurrent += char;\n\t\t} else if ( /\\s/.test( char ) && depth === 0 ) {\n\t\t\tif ( current ) {\n\t\t\t\tparts.push( current );\n\t\t\t\tcurrent = '';\n\t\t\t}\n\t\t} else {\n\t\t\tcurrent += char;\n\t\t}\n\t}\n\n\tif ( current ) {\n\t\tparts.push( current );\n\t}\n\n\treturn parts;\n}\n\n/**\n * Unpacks a 1-to-4 value CSS box shorthand string into individual sides\n * (top, right, bottom, left) following the CSS Box Model specification.\n *\n * @param value The box shorthand value (e.g. `0 0 0 2px`).\n *\n * @return An object containing individual side values.\n */\nexport function unpackBoxShorthand< T extends string | number | undefined >(\n\tvalue: T\n): { top: T; right: T; bottom: T; left: T } {\n\tif ( typeof value !== 'string' ) {\n\t\treturn {\n\t\t\ttop: value,\n\t\t\tright: value,\n\t\t\tbottom: value,\n\t\t\tleft: value,\n\t\t};\n\t}\n\n\tconst parts = splitBoxShorthand( value );\n\n\tswitch ( parts.length ) {\n\t\tcase 2: // [top/bottom, right/left]\n\t\t\treturn {\n\t\t\t\ttop: parts[ 0 ] as T,\n\t\t\t\tright: parts[ 1 ] as T,\n\t\t\t\tbottom: parts[ 0 ] as T,\n\t\t\t\tleft: parts[ 1 ] as T,\n\t\t\t};\n\t\tcase 3: // [top, right/left, bottom]\n\t\t\treturn {\n\t\t\t\ttop: parts[ 0 ] as T,\n\t\t\t\tright: parts[ 1 ] as T,\n\t\t\t\tbottom: parts[ 2 ] as T,\n\t\t\t\tleft: parts[ 1 ] as T,\n\t\t\t};\n\t\tcase 4: // [top, right, bottom, left]\n\t\t\treturn {\n\t\t\t\ttop: parts[ 0 ] as T,\n\t\t\t\tright: parts[ 1 ] as T,\n\t\t\t\tbottom: parts[ 2 ] as T,\n\t\t\t\tleft: parts[ 3 ] as T,\n\t\t\t};\n\t\tdefault:\n\t\t\treturn {\n\t\t\t\ttop: value,\n\t\t\t\tright: value,\n\t\t\t\tbottom: value,\n\t\t\t\tleft: value,\n\t\t\t};\n\t}\n}\n\nexport const getSplitBorders = ( border?: Border ) => {\n\tif ( ! border || isEmptyBorder( border ) ) {\n\t\treturn undefined;\n\t}\n\n\tif ( border.width === undefined ) {\n\t\treturn {\n\t\t\ttop: border,\n\t\t\tright: border,\n\t\t\tbottom: border,\n\t\t\tleft: border,\n\t\t};\n\t}\n\n\tconst unpackedWidths = unpackBoxShorthand( border.width );\n\n\treturn {\n\t\ttop: { ...border, width: unpackedWidths.top },\n\t\tright: { ...border, width: unpackedWidths.right },\n\t\tbottom: { ...border, width: unpackedWidths.bottom },\n\t\tleft: { ...border, width: unpackedWidths.left },\n\t};\n};\n\nexport const getBorderDiff = ( original: Border, updated: Border ) => {\n\tconst diff: Border = {};\n\n\tif ( original.color !== updated.color ) {\n\t\tdiff.color = updated.color;\n\t}\n\n\tif ( original.style !== updated.style ) {\n\t\tdiff.style = updated.style;\n\t}\n\n\tif ( original.width !== updated.width ) {\n\t\tdiff.width = updated.width;\n\t}\n\n\treturn diff;\n};\n\nexport const getCommonBorder = ( borders?: Borders ) => {\n\tif ( ! borders ) {\n\t\treturn undefined;\n\t}\n\n\tconst colors: ( CSSProperties[ 'borderColor' ] | undefined )[] = [];\n\tconst styles: ( CSSProperties[ 'borderStyle' ] | undefined )[] = [];\n\tconst widths: ( CSSProperties[ 'borderWidth' ] | undefined )[] = [];\n\n\tsides.forEach( ( side ) => {\n\t\tcolors.push( borders[ side ]?.color );\n\t\tstyles.push( borders[ side ]?.style );\n\t\twidths.push( borders[ side ]?.width );\n\t} );\n\n\tconst allColorsMatch = colors.every( ( value ) => value === colors[ 0 ] );\n\tconst allStylesMatch = styles.every( ( value ) => value === styles[ 0 ] );\n\tconst allWidthsMatch = widths.every( ( value ) => value === widths[ 0 ] );\n\n\treturn {\n\t\tcolor: allColorsMatch ? colors[ 0 ] : undefined,\n\t\tstyle: allStylesMatch ? styles[ 0 ] : undefined,\n\t\twidth: allWidthsMatch ? widths[ 0 ] : getMostCommonUnit( widths ),\n\t};\n};\n\nexport const getShorthandBorderStyle = (\n\tborder?: Border,\n\tfallbackBorder?: Border\n) => {\n\tif ( isEmptyBorder( border ) ) {\n\t\treturn fallbackBorder;\n\t}\n\n\tconst {\n\t\tcolor: fallbackColor,\n\t\tstyle: fallbackStyle,\n\t\twidth: fallbackWidth,\n\t} = fallbackBorder || {};\n\n\tconst {\n\t\tcolor = fallbackColor,\n\t\tstyle = fallbackStyle,\n\t\twidth = fallbackWidth,\n\t} = border as Border;\n\n\tconst hasVisibleBorder = ( !! width && width !== '0' ) || !! color;\n\tconst borderStyle = hasVisibleBorder ? style || 'solid' : style;\n\n\treturn [ width, borderStyle, color ].filter( Boolean ).join( ' ' );\n};\n\nexport const getMostCommonUnit = (\n\tvalues: Array< string | number | undefined >\n): string | undefined => {\n\t// Collect all the CSS units.\n\tconst units = values.map( ( value ) =>\n\t\tvalue === undefined ? undefined : parseCSSUnitValue( `${ value }` )[ 1 ]\n\t);\n\n\t// Return the most common unit out of only the defined CSS units.\n\tconst filteredUnits = units.filter( ( value ) => value !== undefined );\n\treturn mode( filteredUnits as string[] );\n};\n\n/**\n * Finds the mode value out of the array passed favouring the first value\n * as a tiebreaker.\n *\n * @param values Values to determine the mode from.\n *\n * @return The mode value.\n */\nfunction mode( values: Array< string > ): string | undefined {\n\tif ( values.length === 0 ) {\n\t\treturn undefined;\n\t}\n\n\tconst map: { [ index: string ]: number } = {};\n\tlet maxCount = 0;\n\tlet currentMode;\n\n\tvalues.forEach( ( value ) => {\n\t\tmap[ value ] = map[ value ] === undefined ? 1 : map[ value ] + 1;\n\n\t\tif ( map[ value ] > maxCount ) {\n\t\t\tcurrentMode = value;\n\t\t\tmaxCount = map[ value ];\n\t\t}\n\t} );\n\n\treturn currentMode;\n}\n"],"mappings":";;AAKA,MAAM,QAAsB;CAAE;CAAO;CAAS;CAAU;AAAO;AAC/D,MAAM,cAA4B;CAAE;CAAS;CAAS;AAAQ;AAE9D,MAAa,iBAAkB,WAAqB;CACnD,IAAK,CAAE,QACN,OAAO;CAER,OAAO,CAAE,YAAY,MAAQ,SAAU,OAAQ,UAAW,KAAA,CAAU;AACrE;AAEA,MAAa,mBAAoB,WAAuB;CAEvD,IAAK,CAAE,QACN,OAAO;CAKR,IAAK,gBAAiB,MAAO,GAK5B,OAAO,CAJe,MAAM,OAAS,SACpC,cAAiB,OAAqB,KAAO,CAGzB;CAOtB,OAAO,CAAE,cAAe,MAAiB;AAC1C;AAEA,MAAa,oBAAqB,WAAqB;CACtD,IAAK,CAAE,QACN,OAAO;CAGR,OAAO,YAAY,OAAS,SAAU,OAAQ,UAAW,KAAA,CAAU;AACpE;AAEA,MAAa,mBAAoB,SAAoB,CAAC,MAAO;CAC5D,OAAO,OAAO,KAAM,MAAO,CAAC,CAAC,MAC1B,SAAU,MAAM,QAAS,IAAmB,MAAM,EACrD;AACD;AAEA,MAAa,mBAAoB,YAAwB;CACxD,IAAK,CAAE,gBAAiB,OAAQ,GAC/B,OAAO;CAGR,MAAM,mBAAmB,MAAM,KAAO,SACrC,wBAA2B,UAAwB,KAAO,CAC3D;CAEA,OAAO,CAAE,iBAAiB,OACvB,WAAY,WAAW,iBAAkB,EAC5C;AACD;;;;;;;;;AAUA,SAAgB,kBAAmB,OAA0B;CAC5D,MAAM,QAAkB,CAAC;CACzB,IAAI,UAAU;CACd,IAAI,QAAQ;CAEZ,KAAM,MAAM,QAAQ,MAAM,KAAK,GAC9B,IAAK,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