@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 7.4 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/border-control/border-control/hook.ts"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport * as styles from '../styles';\nimport { parseQuantityAndUnitFromRawValue } from '../../unit-control/utils';\nimport { useContextSystem } from '../../context';\nimport { useCx } from '../../utils/hooks/use-cx';\nimport { maybeWarnDeprecated36pxSize } from '../../utils/deprecated-36px-size';\n\n// If either width or color are defined, the border is considered valid\n// and a border style can be set as well.\nconst isValidBorder = border => {\n const hasWidth = border?.width !== undefined && border.width !== '';\n const hasColor = border?.color !== undefined;\n return hasWidth || hasColor;\n};\nexport function useBorderControl(props) {\n const {\n className,\n colors = [],\n isCompact,\n onChange,\n enableAlpha = true,\n enableStyle = true,\n shouldSanitizeBorder = true,\n size = 'default',\n value: border,\n width,\n __experimentalIsRenderedInSidebar = false,\n __next40pxDefaultSize,\n __shouldNotWarnDeprecated36pxSize,\n ...otherProps\n } = useContextSystem(props, 'BorderControl');\n maybeWarnDeprecated36pxSize({\n componentName: 'BorderControl',\n __next40pxDefaultSize,\n size,\n __shouldNotWarnDeprecated36pxSize\n });\n const computedSize = size === 'default' && __next40pxDefaultSize ? '__unstable-large' : size;\n const [widthValue, originalWidthUnit] = parseQuantityAndUnitFromRawValue(border?.width);\n const widthUnit = originalWidthUnit || 'px';\n const hadPreviousZeroWidth = widthValue === 0;\n const [colorSelection, setColorSelection] = useState();\n const [styleSelection, setStyleSelection] = useState();\n const isStyleSettable = shouldSanitizeBorder ? isValidBorder(border) : true;\n const onBorderChange = useCallback(newBorder => {\n if (shouldSanitizeBorder && !isValidBorder(newBorder)) {\n onChange(undefined);\n return;\n }\n onChange(newBorder);\n }, [onChange, shouldSanitizeBorder]);\n const onWidthChange = useCallback(newWidth => {\n const newWidthValue = newWidth === '' ? undefined : newWidth;\n const [parsedValue] = parseQuantityAndUnitFromRawValue(newWidth);\n const hasZeroWidth = parsedValue === 0;\n const updatedBorder = {\n ...border,\n width: newWidthValue\n };\n\n // Setting the border width explicitly to zero will also set the\n // border style to `none` and clear the border color.\n if (hasZeroWidth && !hadPreviousZeroWidth) {\n // Before clearing the color and style selections, keep track of\n // the current selections so they can be restored when the width\n // changes to a non-zero value.\n setColorSelection(border?.color);\n setStyleSelection(border?.style);\n\n // Clear the color and style border properties.\n updatedBorder.color = undefined;\n updatedBorder.style = 'none';\n }\n\n // Selection has changed from zero border width to non-zero width.\n if (!hasZeroWidth && hadPreviousZeroWidth) {\n // Restore previous border color and style selections if width\n // is now not zero.\n if (updatedBorder.color === undefined) {\n updatedBorder.color = colorSelection;\n }\n if (updatedBorder.style === 'none') {\n updatedBorder.style = styleSelection;\n }\n }\n onBorderChange(updatedBorder);\n }, [border, hadPreviousZeroWidth, colorSelection, styleSelection, onBorderChange]);\n const onSliderChange = useCallback(value => {\n onWidthChange(`${value}${widthUnit}`);\n }, [onWidthChange, widthUnit]);\n\n // Generate class names.\n const cx = useCx();\n const classes = useMemo(() => {\n return cx(styles.borderControl, className);\n }, [className, cx]);\n let wrapperWidth = width;\n if (isCompact) {\n // Widths below represent the minimum usable width for compact controls.\n // Taller controls contain greater internal padding, thus greater width.\n wrapperWidth = size === '__unstable-large' ? '116px' : '90px';\n }\n const innerWrapperClassName = useMemo(() => {\n const widthStyle = !!wrapperWidth && styles.wrapperWidth;\n const heightStyle = styles.wrapperHeight(computedSize);\n return cx(styles.innerWrapper(), widthStyle, heightStyle);\n }, [wrapperWidth, cx, computedSize]);\n const sliderClassName = useMemo(() => {\n return cx(styles.borderSlider());\n }, [cx]);\n return {\n ...otherProps,\n className: classes,\n colors,\n enableAlpha,\n enableStyle,\n innerWrapperClassName,\n inputWidth: wrapperWidth,\n isStyleSettable,\n onBorderChange,\n onSliderChange,\n onWidthChange,\n previousStyleSelection: styleSelection,\n sliderClassName,\n value: border,\n widthUnit,\n widthVal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