fix: prevent asset conflicts between React and Grid.js versions

Add coexistence checks to all enqueue methods to prevent loading
both React and Grid.js assets simultaneously.

Changes:
- ReactAdmin.php: Only enqueue React assets when ?react=1
- Init.php: Skip Grid.js when React active on admin pages
- Form.php, Coupon.php, Access.php: Restore classic assets when ?react=0
- Customer.php, Product.php, License.php: Add coexistence checks

Now the toggle between Classic and React versions works correctly.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-04-18 17:02:14 +07:00
parent bd9cdac02e
commit e8fbfb14c1
74973 changed files with 6658406 additions and 71 deletions

View File

@@ -0,0 +1,95 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _element = require("@wordpress/element");
var _icons = require("@wordpress/icons");
var _i18n = require("@wordpress/i18n");
var _button = _interopRequireDefault(require("../../button"));
var _colorPalette = _interopRequireDefault(require("../../color-palette"));
var _colorIndicator = _interopRequireDefault(require("../../color-indicator"));
var _icon = _interopRequireDefault(require("../../icon"));
var _hStack = require("../../h-stack");
var _compose = require("@wordpress/compose");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function ColorOption({
label,
value,
colors,
disableCustomColors,
enableAlpha,
onChange
}) {
const [isOpen, setIsOpen] = (0, _element.useState)(false);
const idRoot = (0, _compose.useInstanceId)(ColorOption, 'color-list-picker-option');
const labelId = `${idRoot}__label`;
const contentId = `${idRoot}__content`;
return (0, _react.createElement)(_react.Fragment, null, (0, _react.createElement)(_button.default, {
className: "components-color-list-picker__swatch-button",
onClick: () => setIsOpen(prev => !prev),
"aria-expanded": isOpen,
"aria-controls": contentId
}, (0, _react.createElement)(_hStack.HStack, {
justify: "flex-start",
spacing: 2
}, value ? (0, _react.createElement)(_colorIndicator.default, {
colorValue: value,
className: "components-color-list-picker__swatch-color"
}) : (0, _react.createElement)(_icon.default, {
icon: _icons.swatch
}), (0, _react.createElement)("span", {
id: labelId
}, label))), (0, _react.createElement)("div", {
role: "group",
id: contentId,
"aria-labelledby": labelId,
"aria-hidden": !isOpen
}, isOpen && (0, _react.createElement)(_colorPalette.default, {
"aria-label": (0, _i18n.__)('Color options'),
className: "components-color-list-picker__color-picker",
colors: colors,
value: value,
clearable: false,
onChange: onChange,
disableCustomColors: disableCustomColors,
enableAlpha: enableAlpha
})));
}
function ColorListPicker({
colors,
labels,
value = [],
disableCustomColors,
enableAlpha,
onChange
}) {
return (0, _react.createElement)("div", {
className: "components-color-list-picker"
}, labels.map((label, index) => (0, _react.createElement)(ColorOption, {
key: index,
label: label,
value: value[index],
colors: colors,
disableCustomColors: disableCustomColors,
enableAlpha: enableAlpha,
onChange: newColor => {
const newColors = value.slice();
newColors[index] = newColor;
onChange(newColors);
}
})));
}
var _default = ColorListPicker;
exports.default = _default;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":[],"sources":["@wordpress/components/src/duotone-picker/color-list-picker/types.ts"],"sourcesContent":["/**\n * External dependencies\n */\nimport type { CSSProperties } from 'react';\n\nexport type ColorListPickerProps = {\n\t/**\n\t * A list of predefined colors. Each color is an object with a `name` and a\n\t * `color` value.\n\t * The `name` is a string used to identify the color in the UI.\n\t * The `color` is a valid CSS color string.\n\t */\n\tcolors: Array< {\n\t\tname: string;\n\t\tcolor: NonNullable< CSSProperties[ 'color' ] >;\n\t} >;\n\t/**\n\t * A list of labels for each of the options displayed in the UI.\n\t */\n\tlabels: Array< string >;\n\t/**\n\t * An array containing the currently selected colors.\n\t */\n\tvalue?: Array< string >;\n\t/**\n\t * Controls whether the custom color picker is displayed.\n\t */\n\tdisableCustomColors?: boolean;\n\t/**\n\t * Controls whether the ColorPalette should show an alpha channel control.\n\t */\n\tenableAlpha?: boolean;\n\t/**\n\t * A function that receives the updated color value.\n\t */\n\tonChange: ( newValue: Array< string | undefined > ) => void;\n};\n\nexport type ColorOptionProps = Pick<\n\tColorListPickerProps,\n\t'colors' | 'disableCustomColors' | 'enableAlpha'\n> & {\n\tlabel: ColorListPickerProps[ 'labels' ][ number ];\n\tvalue: string | undefined;\n\tonChange: ( newValue: string | undefined ) => void;\n};\n"],"mappings":""}

View File

@@ -0,0 +1,35 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = CustomDuotoneBar;
var _react = require("react");
var _gradientBar = _interopRequireDefault(require("../custom-gradient-picker/gradient-bar"));
var _utils = require("./utils");
/**
* Internal dependencies
*/
const PLACEHOLDER_VALUES = ['#333', '#CCC'];
function CustomDuotoneBar({
value,
onChange
}) {
const hasGradient = !!value;
const values = hasGradient ? value : PLACEHOLDER_VALUES;
const background = (0, _utils.getGradientFromCSSColors)(values);
const controlPoints = (0, _utils.getColorStopsFromColors)(values);
return (0, _react.createElement)(_gradientBar.default, {
disableInserter: true,
background: background,
hasGradient: hasGradient,
value: controlPoints,
onChange: newColorStops => {
const newValue = (0, _utils.getColorsFromColorStops)(newColorStops);
onChange(newValue);
}
});
}
//# sourceMappingURL=custom-duotone-bar.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["_gradientBar","_interopRequireDefault","require","_utils","PLACEHOLDER_VALUES","CustomDuotoneBar","value","onChange","hasGradient","values","background","getGradientFromCSSColors","controlPoints","getColorStopsFromColors","_react","createElement","default","disableInserter","newColorStops","newValue","getColorsFromColorStops"],"sources":["@wordpress/components/src/duotone-picker/custom-duotone-bar.tsx"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport CustomGradientBar from '../custom-gradient-picker/gradient-bar';\n\nimport {\n\tgetColorStopsFromColors,\n\tgetGradientFromCSSColors,\n\tgetColorsFromColorStops,\n} from './utils';\n\nconst PLACEHOLDER_VALUES = [ '#333', '#CCC' ];\n\nexport default function CustomDuotoneBar( {\n\tvalue,\n\tonChange,\n}: {\n\tvalue?: string[];\n\tonChange: ( value?: string[] ) => void;\n} ) {\n\tconst hasGradient = !! value;\n\tconst values = hasGradient ? value : PLACEHOLDER_VALUES;\n\tconst background = getGradientFromCSSColors( values );\n\tconst controlPoints = getColorStopsFromColors( values );\n\treturn (\n\t\t<CustomGradientBar\n\t\t\tdisableInserter\n\t\t\tbackground={ background }\n\t\t\thasGradient={ hasGradient }\n\t\t\tvalue={ controlPoints }\n\t\t\tonChange={ ( newColorStops ) => {\n\t\t\t\tconst newValue = getColorsFromColorStops( newColorStops );\n\t\t\t\tonChange( newValue );\n\t\t\t} }\n\t\t/>\n\t);\n}\n"],"mappings":";;;;;;;;AAGA,IAAAA,YAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AALA;AACA;AACA;;AASA,MAAME,kBAAkB,GAAG,CAAE,MAAM,EAAE,MAAM,CAAE;AAE9B,SAASC,gBAAgBA,CAAE;EACzCC,KAAK;EACLC;AAID,CAAC,EAAG;EACH,MAAMC,WAAW,GAAG,CAAC,CAAEF,KAAK;EAC5B,MAAMG,MAAM,GAAGD,WAAW,GAAGF,KAAK,GAAGF,kBAAkB;EACvD,MAAMM,UAAU,GAAG,IAAAC,+BAAwB,EAAEF,MAAO,CAAC;EACrD,MAAMG,aAAa,GAAG,IAAAC,8BAAuB,EAAEJ,MAAO,CAAC;EACvD,OACC,IAAAK,MAAA,CAAAC,aAAA,EAACf,YAAA,CAAAgB,OAAiB;IACjBC,eAAe;IACfP,UAAU,EAAGA,UAAY;IACzBF,WAAW,EAAGA,WAAa;IAC3BF,KAAK,EAAGM,aAAe;IACvBL,QAAQ,EAAKW,aAAa,IAAM;MAC/B,MAAMC,QAAQ,GAAG,IAAAC,8BAAuB,EAAEF,aAAc,CAAC;MACzDX,QAAQ,CAAEY,QAAS,CAAC;IACrB;EAAG,CACH,CAAC;AAEJ"}

View File

@@ -0,0 +1,185 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _es = _interopRequireDefault(require("fast-deep-equal/es6"));
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _colorListPicker = _interopRequireDefault(require("./color-list-picker"));
var _circularOptionPicker = _interopRequireDefault(require("../circular-option-picker"));
var _vStack = require("../v-stack");
var _customDuotoneBar = _interopRequireDefault(require("./custom-duotone-bar"));
var _utils = require("./utils");
var _spacer = require("../spacer");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* ```jsx
* import { DuotonePicker, DuotoneSwatch } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const DUOTONE_PALETTE = [
* { colors: [ '#8c00b7', '#fcff41' ], name: 'Purple and yellow', slug: 'purple-yellow' },
* { colors: [ '#000097', '#ff4747' ], name: 'Blue and red', slug: 'blue-red' },
* ];
*
* const COLOR_PALETTE = [
* { color: '#ff4747', name: 'Red', slug: 'red' },
* { color: '#fcff41', name: 'Yellow', slug: 'yellow' },
* { color: '#000097', name: 'Blue', slug: 'blue' },
* { color: '#8c00b7', name: 'Purple', slug: 'purple' },
* ];
*
* const Example = () => {
* const [ duotone, setDuotone ] = useState( [ '#000000', '#ffffff' ] );
* return (
* <>
* <DuotonePicker
* duotonePalette={ DUOTONE_PALETTE }
* colorPalette={ COLOR_PALETTE }
* value={ duotone }
* onChange={ setDuotone }
* />
* <DuotoneSwatch values={ duotone } />
* </>
* );
* };
* ```
*/
function DuotonePicker({
asButtons,
loop,
clearable = true,
unsetable = true,
colorPalette,
duotonePalette,
disableCustomColors,
disableCustomDuotone,
value,
onChange,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
...otherProps
}) {
const [defaultDark, defaultLight] = (0, _element.useMemo)(() => (0, _utils.getDefaultColors)(colorPalette), [colorPalette]);
const isUnset = value === 'unset';
const unsetOptionLabel = (0, _i18n.__)('Unset');
const unsetOption = (0, _react.createElement)(_circularOptionPicker.default.Option, {
key: "unset",
value: "unset",
isSelected: isUnset,
tooltipText: unsetOptionLabel,
"aria-label": unsetOptionLabel,
className: "components-duotone-picker__color-indicator",
onClick: () => {
onChange(isUnset ? undefined : 'unset');
}
});
const duotoneOptions = duotonePalette.map(({
colors,
slug,
name
}) => {
const style = {
background: (0, _utils.getGradientFromCSSColors)(colors, '135deg'),
color: 'transparent'
};
const tooltipText = name !== null && name !== void 0 ? name : (0, _i18n.sprintf)(
// translators: %s: duotone code e.g: "dark-grayscale" or "7f7f7f-ffffff".
(0, _i18n.__)('Duotone code: %s'), slug);
const label = name ? (0, _i18n.sprintf)(
// translators: %s: The name of the option e.g: "Dark grayscale".
(0, _i18n.__)('Duotone: %s'), name) : tooltipText;
const isSelected = (0, _es.default)(colors, value);
return (0, _react.createElement)(_circularOptionPicker.default.Option, {
key: slug,
value: colors,
isSelected: isSelected,
"aria-label": label,
tooltipText: tooltipText,
style: style,
onClick: () => {
onChange(isSelected ? undefined : colors);
}
});
});
let metaProps;
if (asButtons) {
metaProps = {
asButtons: true
};
} else {
const _metaProps = {
asButtons: false,
loop
};
if (ariaLabel) {
metaProps = {
..._metaProps,
'aria-label': ariaLabel
};
} else if (ariaLabelledby) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby
};
} else {
metaProps = {
..._metaProps,
'aria-label': (0, _i18n.__)('Custom color picker.')
};
}
}
const options = unsetable ? [unsetOption, ...duotoneOptions] : duotoneOptions;
return (0, _react.createElement)(_circularOptionPicker.default, {
...otherProps,
...metaProps,
options: options,
actions: !!clearable && (0, _react.createElement)(_circularOptionPicker.default.ButtonAction, {
onClick: () => onChange(undefined)
}, (0, _i18n.__)('Clear'))
}, (0, _react.createElement)(_spacer.Spacer, {
paddingTop: options.length === 0 ? 0 : 4
}, (0, _react.createElement)(_vStack.VStack, {
spacing: 3
}, !disableCustomColors && !disableCustomDuotone && (0, _react.createElement)(_customDuotoneBar.default, {
value: isUnset ? undefined : value,
onChange: onChange
}), !disableCustomDuotone && (0, _react.createElement)(_colorListPicker.default, {
labels: [(0, _i18n.__)('Shadows'), (0, _i18n.__)('Highlights')],
colors: colorPalette,
value: isUnset ? undefined : value,
disableCustomColors: disableCustomColors,
enableAlpha: true,
onChange: newColors => {
if (!newColors[0]) {
newColors[0] = defaultDark;
}
if (!newColors[1]) {
newColors[1] = defaultLight;
}
const newValue = newColors.length >= 2 ? newColors : undefined;
// @ts-expect-error TODO: The color arrays for a DuotonePicker should be a tuple of two colors,
// but it's currently typed as a string[].
// See also https://github.com/WordPress/gutenberg/pull/49060#discussion_r1136951035
onChange(newValue);
}
}))));
}
var _default = DuotonePicker;
exports.default = _default;
//# sourceMappingURL=duotone-picker.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,32 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _icons = require("@wordpress/icons");
var _colorIndicator = _interopRequireDefault(require("../color-indicator"));
var _icon = _interopRequireDefault(require("../icon"));
var _utils = require("./utils");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function DuotoneSwatch({
values
}) {
return values ? (0, _react.createElement)(_colorIndicator.default, {
colorValue: (0, _utils.getGradientFromCSSColors)(values, '135deg')
}) : (0, _react.createElement)(_icon.default, {
icon: _icons.swatch
});
}
var _default = DuotoneSwatch;
exports.default = _default;
//# sourceMappingURL=duotone-swatch.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["_icons","require","_colorIndicator","_interopRequireDefault","_icon","_utils","DuotoneSwatch","values","_react","createElement","default","colorValue","getGradientFromCSSColors","icon","swatch","_default","exports"],"sources":["@wordpress/components/src/duotone-picker/duotone-swatch.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { swatch } from '@wordpress/icons';\n\n/**\n * Internal dependencies\n */\nimport ColorIndicator from '../color-indicator';\nimport Icon from '../icon';\nimport { getGradientFromCSSColors } from './utils';\nimport type { DuotoneSwatchProps } from './types';\n\nfunction DuotoneSwatch( { values }: DuotoneSwatchProps ) {\n\treturn values ? (\n\t\t<ColorIndicator\n\t\t\tcolorValue={ getGradientFromCSSColors( values, '135deg' ) }\n\t\t/>\n\t) : (\n\t\t<Icon icon={ swatch } />\n\t);\n}\n\nexport default DuotoneSwatch;\n"],"mappings":";;;;;;;;AAGA,IAAAA,MAAA,GAAAC,OAAA;AAKA,IAAAC,eAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,KAAA,GAAAD,sBAAA,CAAAF,OAAA;AACA,IAAAI,MAAA,GAAAJ,OAAA;AAVA;AACA;AACA;;AAGA;AACA;AACA;;AAMA,SAASK,aAAaA,CAAE;EAAEC;AAA2B,CAAC,EAAG;EACxD,OAAOA,MAAM,GACZ,IAAAC,MAAA,CAAAC,aAAA,EAACP,eAAA,CAAAQ,OAAc;IACdC,UAAU,EAAG,IAAAC,+BAAwB,EAAEL,MAAM,EAAE,QAAS;EAAG,CAC3D,CAAC,GAEF,IAAAC,MAAA,CAAAC,aAAA,EAACL,KAAA,CAAAM,OAAI;IAACG,IAAI,EAAGC;EAAQ,CAAE,CACvB;AACF;AAAC,IAAAC,QAAA,GAEcT,aAAa;AAAAU,OAAA,CAAAN,OAAA,GAAAK,QAAA"}

View File

@@ -0,0 +1,21 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "DuotonePicker", {
enumerable: true,
get: function () {
return _duotonePicker.default;
}
});
Object.defineProperty(exports, "DuotoneSwatch", {
enumerable: true,
get: function () {
return _duotoneSwatch.default;
}
});
var _duotonePicker = _interopRequireDefault(require("./duotone-picker"));
var _duotoneSwatch = _interopRequireDefault(require("./duotone-swatch"));
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["_duotonePicker","_interopRequireDefault","require","_duotoneSwatch"],"sources":["@wordpress/components/src/duotone-picker/index.ts"],"sourcesContent":["export { default as DuotonePicker } from './duotone-picker';\nexport { default as DuotoneSwatch } from './duotone-swatch';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,IAAAA,cAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,cAAA,GAAAF,sBAAA,CAAAC,OAAA"}

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":[],"sources":["@wordpress/components/src/duotone-picker/types.ts"],"sourcesContent":["export type DuotonePickerProps = {\n\t/**\n\t * Whether there should be a button to clear the duotone value.\n\t *\n\t * @default true\n\t */\n\tclearable?: boolean;\n\t/**\n\t * Whether there should be an `unset` option.\n\t *\n\t * @default true\n\t */\n\tunsetable?: boolean;\n\t/**\n\t * Array of color presets of the form `{ color: '#000000', name: 'Black', slug: 'black' }`.\n\t */\n\tcolorPalette: Color[];\n\t/**\n\t * Array of duotone presets of the form `{ colors: [ '#000000', '#ffffff' ], name: 'Grayscale', slug: 'grayscale' }`.\n\t */\n\tduotonePalette: DuotoneColor[];\n\t/**\n\t * Whether custom colors should be disabled.\n\t *\n\t * @default false\n\t */\n\tdisableCustomColors?: boolean;\n\t/**\n\t * Whether custom duotone values should be disabled.\n\t *\n\t * @default false\n\t */\n\tdisableCustomDuotone?: boolean;\n\t/**\n\t * An array of colors for the duotone effect.\n\t */\n\tvalue?: string[] | 'unset';\n\t/**\n\t * Callback which is called when the duotone colors change.\n\t */\n\tonChange: ( value: DuotonePickerProps[ 'value' ] | undefined ) => void;\n\t/**\n\t * Whether the control should present as a set of buttons,\n\t * each with its own tab stop.\n\t *\n\t * @default false\n\t */\n\tasButtons?: boolean;\n\t/**\n\t * Prevents keyboard interaction from wrapping around.\n\t * Only used when `asButtons` is not true.\n\t *\n\t * @default true\n\t */\n\tloop?: boolean;\n} & (\n\t| {\n\t\t\t/**\n\t\t\t * A label to identify the purpose of the control.\n\t\t\t *\n\t\t\t * @todo [#54055] Either this or `aria-labelledby` should be required\n\t\t\t */\n\t\t\t'aria-label'?: string;\n\t\t\t'aria-labelledby'?: never;\n\t }\n\t| {\n\t\t\t/**\n\t\t\t * An ID of an element to provide a label for the control.\n\t\t\t *\n\t\t\t * @todo [#54055] Either this or `aria-label` should be required\n\t\t\t */\n\t\t\t'aria-labelledby'?: string;\n\t\t\t'aria-label'?: never;\n\t }\n);\n\ntype Color = {\n\tcolor: string;\n\tname: string;\n\tslug: string;\n};\n\ntype DuotoneColor = {\n\tcolors: string[];\n\tname: string;\n\tslug: string;\n};\n\nexport type DuotoneSwatchProps = {\n\t/**\n\t * An array of colors to show or `null` to show the placeholder swatch icon.\n\t */\n\tvalues?: string[] | null;\n};\n"],"mappings":""}

View File

@@ -0,0 +1,96 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getColorStopsFromColors = getColorStopsFromColors;
exports.getColorsFromColorStops = getColorsFromColorStops;
exports.getDefaultColors = getDefaultColors;
exports.getGradientFromCSSColors = getGradientFromCSSColors;
var _colord = require("colord");
var _names = _interopRequireDefault(require("colord/plugins/names"));
/**
* External dependencies
*/
(0, _colord.extend)([_names.default]);
/**
* Object representation for a color.
*
* @typedef {Object} RGBColor
* @property {number} r Red component of the color in the range [0,1].
* @property {number} g Green component of the color in the range [0,1].
* @property {number} b Blue component of the color in the range [0,1].
*/
/**
* Calculate the brightest and darkest values from a color palette.
*
* @param palette Color palette for the theme.
*
* @return Tuple of the darkest color and brightest color.
*/
function getDefaultColors(palette) {
// A default dark and light color are required.
if (!palette || palette.length < 2) return ['#000', '#fff'];
return palette.map(({
color
}) => ({
color,
brightness: (0, _colord.colord)(color).brightness()
})).reduce(([min, max], current) => {
return [current.brightness <= min.brightness ? current : min, current.brightness >= max.brightness ? current : max];
}, [{
brightness: 1,
color: ''
}, {
brightness: 0,
color: ''
}]).map(({
color
}) => color);
}
/**
* Generate a duotone gradient from a list of colors.
*
* @param colors CSS color strings.
* @param angle CSS gradient angle.
*
* @return CSS gradient string for the duotone swatch.
*/
function getGradientFromCSSColors(colors = [], angle = '90deg') {
const l = 100 / colors.length;
const stops = colors.map((c, i) => `${c} ${i * l}%, ${c} ${(i + 1) * l}%`).join(', ');
return `linear-gradient( ${angle}, ${stops} )`;
}
/**
* Convert a color array to an array of color stops.
*
* @param colors CSS colors array
*
* @return Color stop information.
*/
function getColorStopsFromColors(colors) {
return colors.map((color, i) => ({
position: i * 100 / (colors.length - 1),
color
}));
}
/**
* Convert a color stop array to an array colors.
*
* @param colorStops Color stop information.
*
* @return CSS colors array.
*/
function getColorsFromColorStops(colorStops = []) {
return colorStops.map(({
color
}) => color);
}
//# sourceMappingURL=utils.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["_colord","require","_names","_interopRequireDefault","extend","namesPlugin","getDefaultColors","palette","length","map","color","brightness","colord","reduce","min","max","current","getGradientFromCSSColors","colors","angle","l","stops","c","i","join","getColorStopsFromColors","position","getColorsFromColorStops","colorStops"],"sources":["@wordpress/components/src/duotone-picker/utils.ts"],"sourcesContent":["/**\n * External dependencies\n */\nimport { colord, extend } from 'colord';\nimport namesPlugin from 'colord/plugins/names';\n\n/**\n * Internal dependencies\n */\nimport type { DuotonePickerProps } from './types';\n\nextend( [ namesPlugin ] );\n\n/**\n * Object representation for a color.\n *\n * @typedef {Object} RGBColor\n * @property {number} r Red component of the color in the range [0,1].\n * @property {number} g Green component of the color in the range [0,1].\n * @property {number} b Blue component of the color in the range [0,1].\n */\n\n/**\n * Calculate the brightest and darkest values from a color palette.\n *\n * @param palette Color palette for the theme.\n *\n * @return Tuple of the darkest color and brightest color.\n */\nexport function getDefaultColors(\n\tpalette: DuotonePickerProps[ 'colorPalette' ]\n) {\n\t// A default dark and light color are required.\n\tif ( ! palette || palette.length < 2 ) return [ '#000', '#fff' ];\n\n\treturn palette\n\t\t.map( ( { color } ) => ( {\n\t\t\tcolor,\n\t\t\tbrightness: colord( color ).brightness(),\n\t\t} ) )\n\t\t.reduce(\n\t\t\t( [ min, max ], current ) => {\n\t\t\t\treturn [\n\t\t\t\t\tcurrent.brightness <= min.brightness ? current : min,\n\t\t\t\t\tcurrent.brightness >= max.brightness ? current : max,\n\t\t\t\t];\n\t\t\t},\n\t\t\t[\n\t\t\t\t{ brightness: 1, color: '' },\n\t\t\t\t{ brightness: 0, color: '' },\n\t\t\t]\n\t\t)\n\t\t.map( ( { color } ) => color );\n}\n\n/**\n * Generate a duotone gradient from a list of colors.\n *\n * @param colors CSS color strings.\n * @param angle CSS gradient angle.\n *\n * @return CSS gradient string for the duotone swatch.\n */\nexport function getGradientFromCSSColors(\n\tcolors: string[] = [],\n\tangle = '90deg'\n) {\n\tconst l = 100 / colors.length;\n\n\tconst stops = colors\n\t\t.map( ( c, i ) => `${ c } ${ i * l }%, ${ c } ${ ( i + 1 ) * l }%` )\n\t\t.join( ', ' );\n\n\treturn `linear-gradient( ${ angle }, ${ stops } )`;\n}\n\n/**\n * Convert a color array to an array of color stops.\n *\n * @param colors CSS colors array\n *\n * @return Color stop information.\n */\nexport function getColorStopsFromColors( colors: string[] ) {\n\treturn colors.map( ( color, i ) => ( {\n\t\tposition: ( i * 100 ) / ( colors.length - 1 ),\n\t\tcolor,\n\t} ) );\n}\n\n/**\n * Convert a color stop array to an array colors.\n *\n * @param colorStops Color stop information.\n *\n * @return CSS colors array.\n */\nexport function getColorsFromColorStops(\n\tcolorStops: { position: number; color: string }[] = []\n) {\n\treturn colorStops.map( ( { color } ) => color );\n}\n"],"mappings":";;;;;;;;;;AAGA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AAJA;AACA;AACA;;AASA,IAAAG,cAAM,EAAE,CAAEC,cAAW,CAAG,CAAC;;AAEzB;AACA;AACA;AACA;AACA;AACA;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