@wordpress/components 23.2.0 → 23.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -1
- package/CONTRIBUTING.md +1 -1
- package/build/alignment-matrix-control/utils.js +2 -2
- package/build/alignment-matrix-control/utils.js.map +1 -1
- package/build/autocomplete/autocompleter-ui.js +1 -3
- package/build/autocomplete/autocompleter-ui.js.map +1 -1
- package/build/border-box-control/border-box-control-linked-button/component.js.map +1 -1
- package/build/border-box-control/border-box-control-linked-button/hook.js.map +1 -1
- package/build/border-control/border-control-dropdown/component.js +8 -4
- package/build/border-control/border-control-dropdown/component.js.map +1 -1
- package/build/button/deprecated.js +8 -6
- package/build/button/deprecated.js.map +1 -1
- package/build/button/index.js +52 -23
- package/build/button/index.js.map +1 -1
- package/build/button/types.js +6 -0
- package/build/button/types.js.map +1 -0
- package/build/color-list-picker/index.js.map +1 -1
- package/build/color-list-picker/types.js +6 -0
- package/build/color-list-picker/types.js.map +1 -0
- package/build/color-palette/index.js +9 -61
- package/build/color-palette/index.js.map +1 -1
- package/build/color-palette/index.native.js +24 -9
- package/build/color-palette/index.native.js.map +1 -1
- package/build/color-palette/utils.js +103 -0
- package/build/color-palette/utils.js.map +1 -0
- package/build/custom-gradient-picker/gradient-bar/utils.js +1 -1
- package/build/custom-gradient-picker/gradient-bar/utils.js.map +1 -1
- package/build/date-time/date/index.js.map +1 -1
- package/build/dropdown/index.js +20 -8
- package/build/dropdown/index.js.map +1 -1
- package/build/form-token-field/token.js +1 -1
- package/build/form-token-field/token.js.map +1 -1
- package/build/gradient-picker/index.js +9 -1
- package/build/gradient-picker/index.js.map +1 -1
- package/build/h-stack/component.js +0 -1
- package/build/h-stack/component.js.map +1 -1
- package/build/input-control/input-field.js +4 -2
- package/build/input-control/input-field.js.map +1 -1
- package/build/keyboard-shortcuts/index.js +44 -16
- package/build/keyboard-shortcuts/index.js.map +1 -1
- package/build/keyboard-shortcuts/types.js +6 -0
- package/build/keyboard-shortcuts/types.js.map +1 -0
- package/build/modal/index.js +1 -1
- package/build/modal/index.js.map +1 -1
- package/build/notice/index.js +16 -18
- package/build/notice/index.js.map +1 -1
- package/build/notice/list.js +23 -8
- package/build/notice/list.js.map +1 -1
- package/build/notice/types.js +6 -0
- package/build/notice/types.js.map +1 -0
- package/build/number-control/index.js +1 -1
- package/build/number-control/index.js.map +1 -1
- package/build/query-controls/author-select.js +7 -3
- package/build/query-controls/author-select.js.map +1 -1
- package/build/query-controls/category-select.js +7 -3
- package/build/query-controls/category-select.js.map +1 -1
- package/build/query-controls/index.js +68 -20
- package/build/query-controls/index.js.map +1 -1
- package/build/query-controls/terms.js +4 -3
- package/build/query-controls/terms.js.map +1 -1
- package/build/query-controls/types.js +6 -0
- package/build/query-controls/types.js.map +1 -0
- package/build/slot-fill/bubbles-virtually/fill.js +1 -0
- package/build/slot-fill/bubbles-virtually/fill.js.map +1 -1
- package/build/slot-fill/slot.js +1 -0
- package/build/slot-fill/slot.js.map +1 -1
- package/build/slot-fill/use-slot.js +1 -11
- package/build/slot-fill/use-slot.js.map +1 -1
- package/build/snackbar/index.js.map +1 -1
- package/build/snackbar/list.js.map +1 -1
- package/build/tab-panel/index.js +36 -8
- package/build/tab-panel/index.js.map +1 -1
- package/build/tree-grid/index.js +1 -1
- package/build/tree-grid/index.js.map +1 -1
- package/build/tree-select/index.js +2 -6
- package/build/tree-select/index.js.map +1 -1
- package/build-module/alignment-matrix-control/utils.js +2 -2
- package/build-module/alignment-matrix-control/utils.js.map +1 -1
- package/build-module/autocomplete/autocompleter-ui.js +1 -2
- package/build-module/autocomplete/autocompleter-ui.js.map +1 -1
- package/build-module/border-box-control/border-box-control-linked-button/component.js.map +1 -1
- package/build-module/border-box-control/border-box-control-linked-button/hook.js.map +1 -1
- package/build-module/border-control/border-control-dropdown/component.js +7 -4
- package/build-module/border-control/border-control-dropdown/component.js.map +1 -1
- package/build-module/button/deprecated.js +8 -5
- package/build-module/button/deprecated.js.map +1 -1
- package/build-module/button/index.js +51 -22
- package/build-module/button/index.js.map +1 -1
- package/build-module/button/types.js +2 -0
- package/build-module/button/types.js.map +1 -0
- package/build-module/color-list-picker/index.js.map +1 -1
- package/build-module/color-list-picker/types.js +2 -0
- package/build-module/color-list-picker/types.js.map +1 -0
- package/build-module/color-palette/index.js +7 -54
- package/build-module/color-palette/index.js.map +1 -1
- package/build-module/color-palette/index.native.js +24 -8
- package/build-module/color-palette/index.native.js.map +1 -1
- package/build-module/color-palette/utils.js +79 -0
- package/build-module/color-palette/utils.js.map +1 -0
- package/build-module/custom-gradient-picker/gradient-bar/utils.js +1 -1
- package/build-module/custom-gradient-picker/gradient-bar/utils.js.map +1 -1
- package/build-module/date-time/date/index.js +1 -1
- package/build-module/date-time/date/index.js.map +1 -1
- package/build-module/dropdown/index.js +19 -8
- package/build-module/dropdown/index.js.map +1 -1
- package/build-module/form-token-field/token.js +1 -1
- package/build-module/form-token-field/token.js.map +1 -1
- package/build-module/gradient-picker/index.js +9 -2
- package/build-module/gradient-picker/index.js.map +1 -1
- package/build-module/h-stack/component.js +0 -1
- package/build-module/h-stack/component.js.map +1 -1
- package/build-module/input-control/input-field.js +4 -2
- package/build-module/input-control/input-field.js.map +1 -1
- package/build-module/keyboard-shortcuts/index.js +48 -16
- package/build-module/keyboard-shortcuts/index.js.map +1 -1
- package/build-module/keyboard-shortcuts/types.js +2 -0
- package/build-module/keyboard-shortcuts/types.js.map +1 -0
- package/build-module/modal/index.js +1 -1
- package/build-module/modal/index.js.map +1 -1
- package/build-module/notice/index.js +14 -15
- package/build-module/notice/index.js.map +1 -1
- package/build-module/notice/list.js +23 -8
- package/build-module/notice/list.js.map +1 -1
- package/build-module/notice/types.js +2 -0
- package/build-module/notice/types.js.map +1 -0
- package/build-module/number-control/index.js +1 -1
- package/build-module/number-control/index.js.map +1 -1
- package/build-module/query-controls/author-select.js +7 -3
- package/build-module/query-controls/author-select.js.map +1 -1
- package/build-module/query-controls/category-select.js +8 -4
- package/build-module/query-controls/category-select.js.map +1 -1
- package/build-module/query-controls/index.js +64 -20
- package/build-module/query-controls/index.js.map +1 -1
- package/build-module/query-controls/terms.js +8 -4
- package/build-module/query-controls/terms.js.map +1 -1
- package/build-module/query-controls/types.js +2 -0
- package/build-module/query-controls/types.js.map +1 -0
- package/build-module/slot-fill/bubbles-virtually/fill.js +1 -0
- package/build-module/slot-fill/bubbles-virtually/fill.js.map +1 -1
- package/build-module/slot-fill/slot.js +1 -0
- package/build-module/slot-fill/slot.js.map +1 -1
- package/build-module/slot-fill/use-slot.js +2 -12
- package/build-module/slot-fill/use-slot.js.map +1 -1
- package/build-module/snackbar/index.js.map +1 -1
- package/build-module/snackbar/list.js.map +1 -1
- package/build-module/tab-panel/index.js +36 -8
- package/build-module/tab-panel/index.js.map +1 -1
- package/build-module/tree-grid/index.js +1 -1
- package/build-module/tree-grid/index.js.map +1 -1
- package/build-module/tree-select/index.js +2 -6
- package/build-module/tree-select/index.js.map +1 -1
- package/build-style/style-rtl.css +5 -0
- package/build-style/style.css +5 -0
- package/build-types/border-box-control/border-box-control/hook.d.ts +1 -1
- package/build-types/border-box-control/border-box-control-linked-button/component.d.ts +1 -1
- package/build-types/border-box-control/border-box-control-linked-button/hook.d.ts +171 -160
- package/build-types/border-box-control/border-box-control-linked-button/hook.d.ts.map +1 -1
- package/build-types/border-box-control/border-box-control-split-controls/hook.d.ts +1 -1
- package/build-types/border-control/border-control/hook.d.ts +1 -1
- package/build-types/border-control/border-control-dropdown/component.d.ts.map +1 -1
- package/build-types/border-control/border-control-dropdown/hook.d.ts +1 -1
- package/build-types/button/deprecated.d.ts +143 -7
- package/build-types/button/deprecated.d.ts.map +1 -1
- package/build-types/button/index.d.ts +20 -3
- package/build-types/button/index.d.ts.map +1 -1
- package/build-types/button/stories/index.d.ts +20 -0
- package/build-types/button/stories/index.d.ts.map +1 -0
- package/build-types/button/test/index.d.ts +2 -0
- package/build-types/button/test/index.d.ts.map +1 -0
- package/build-types/button/types.d.ts +134 -0
- package/build-types/button/types.d.ts.map +1 -0
- package/build-types/color-list-picker/index.d.ts +5 -0
- package/build-types/color-list-picker/index.d.ts.map +1 -0
- package/build-types/color-list-picker/types.d.ts +42 -0
- package/build-types/color-list-picker/types.d.ts.map +1 -0
- package/build-types/color-palette/index.d.ts +2 -4
- package/build-types/color-palette/index.d.ts.map +1 -1
- package/build-types/color-palette/stories/index.d.ts +2 -2
- package/build-types/color-palette/styles.d.ts +1 -1
- package/build-types/color-palette/types.d.ts +1 -1
- package/build-types/color-palette/types.d.ts.map +1 -1
- package/build-types/color-palette/utils.d.ts +14 -0
- package/build-types/color-palette/utils.d.ts.map +1 -0
- package/build-types/color-picker/styles.d.ts +2 -2
- package/build-types/date-time/date/index.d.ts.map +1 -1
- package/build-types/date-time/date/styles.d.ts +3 -3
- package/build-types/dropdown/index.d.ts +4 -4
- package/build-types/dropdown/index.d.ts.map +1 -1
- package/build-types/dropdown/stories/index.d.ts.map +1 -1
- package/build-types/dropdown/types.d.ts +9 -10
- package/build-types/dropdown/types.d.ts.map +1 -1
- package/build-types/font-size-picker/styles.d.ts +2 -2
- package/build-types/h-stack/component.d.ts +0 -1
- package/build-types/h-stack/component.d.ts.map +1 -1
- package/build-types/input-control/input-field.d.ts.map +1 -1
- package/build-types/keyboard-shortcuts/index.d.ts +38 -0
- package/build-types/keyboard-shortcuts/index.d.ts.map +1 -0
- package/build-types/keyboard-shortcuts/stories/index.d.ts +12 -0
- package/build-types/keyboard-shortcuts/stories/index.d.ts.map +1 -0
- package/build-types/keyboard-shortcuts/test/index.d.ts +2 -0
- package/build-types/keyboard-shortcuts/test/index.d.ts.map +1 -0
- package/build-types/keyboard-shortcuts/types.d.ts +48 -0
- package/build-types/keyboard-shortcuts/types.d.ts.map +1 -0
- package/build-types/modal/index.d.ts.map +1 -1
- package/build-types/navigator/navigator-back-button/component.d.ts +1 -1
- package/build-types/navigator/navigator-back-button/hook.d.ts +2 -2
- package/build-types/navigator/navigator-button/component.d.ts +1 -1
- package/build-types/navigator/navigator-button/hook.d.ts +2 -2
- package/build-types/notice/index.d.ts +16 -0
- package/build-types/notice/index.d.ts.map +1 -0
- package/build-types/notice/list.d.ts +32 -0
- package/build-types/notice/list.d.ts.map +1 -0
- package/build-types/notice/stories/index.d.ts +17 -0
- package/build-types/notice/stories/index.d.ts.map +1 -0
- package/build-types/notice/test/index.d.ts +2 -0
- package/build-types/notice/test/index.d.ts.map +1 -0
- package/build-types/notice/test/list.d.ts +2 -0
- package/build-types/notice/test/list.d.ts.map +1 -0
- package/build-types/notice/types.d.ts +128 -0
- package/build-types/notice/types.d.ts.map +1 -0
- package/build-types/number-control/styles/number-control-styles.d.ts +2 -2
- package/build-types/number-control/styles/number-control-styles.d.ts.map +1 -1
- package/build-types/placeholder/stories/index.d.ts.map +1 -1
- package/build-types/query-controls/author-select.d.ts +4 -0
- package/build-types/query-controls/author-select.d.ts.map +1 -0
- package/build-types/query-controls/category-select.d.ts +4 -0
- package/build-types/query-controls/category-select.d.ts.map +1 -0
- package/build-types/query-controls/index.d.ts +30 -0
- package/build-types/query-controls/index.d.ts.map +1 -0
- package/build-types/query-controls/stories/index.d.ts +13 -0
- package/build-types/query-controls/stories/index.d.ts.map +1 -0
- package/build-types/query-controls/terms.d.ts +13 -0
- package/build-types/query-controls/terms.d.ts.map +1 -0
- package/build-types/query-controls/test/terms.d.ts +2 -0
- package/build-types/query-controls/test/terms.d.ts.map +1 -0
- package/build-types/query-controls/types.d.ts +131 -0
- package/build-types/query-controls/types.d.ts.map +1 -0
- package/build-types/range-control/index.d.ts +1 -1
- package/build-types/slot-fill/bubbles-virtually/fill.d.ts.map +1 -1
- package/build-types/slot-fill/use-slot.d.ts.map +1 -1
- package/build-types/snackbar/index.d.ts +9 -2
- package/build-types/snackbar/index.d.ts.map +1 -1
- package/build-types/snackbar/list.d.ts.map +1 -1
- package/build-types/snackbar/types.d.ts +15 -88
- package/build-types/snackbar/types.d.ts.map +1 -1
- package/build-types/tab-panel/index.d.ts.map +1 -1
- package/build-types/tab-panel/types.d.ts +1 -1
- package/build-types/tab-panel/types.d.ts.map +1 -1
- package/build-types/toggle-group-control/stories/index.d.ts.map +1 -1
- package/build-types/tree-select/index.d.ts.map +1 -1
- package/build-types/ui/form-group/form-group.d.ts +2 -2
- package/package.json +18 -17
- package/src/alignment-matrix-control/utils.tsx +2 -2
- package/src/autocomplete/autocompleter-ui.js +1 -2
- package/src/autocomplete/test/index.js +1 -5
- package/src/border-box-control/border-box-control-linked-button/component.tsx +1 -1
- package/src/border-box-control/border-box-control-linked-button/hook.ts +1 -1
- package/src/border-control/border-control-dropdown/component.tsx +9 -8
- package/src/box-control/test/index.js +11 -35
- package/src/button/README.md +49 -55
- package/src/button/{deprecated.js → deprecated.tsx} +19 -4
- package/src/button/{index.js → index.tsx} +95 -34
- package/src/button/stories/index.tsx +106 -0
- package/src/button/style.scss +3 -2
- package/src/button/test/{index.js → index.tsx} +30 -7
- package/src/button/types.ts +138 -0
- package/src/checkbox-control/test/index.tsx +1 -5
- package/src/color-list-picker/{index.js → index.tsx} +3 -2
- package/src/color-list-picker/types.ts +46 -0
- package/src/color-palette/README.md +1 -1
- package/src/color-palette/index.native.js +11 -4
- package/src/color-palette/index.tsx +11 -67
- package/src/color-palette/test/index.tsx +4 -14
- package/src/color-palette/test/utils.ts +1 -1
- package/src/color-palette/types.ts +1 -1
- package/src/color-palette/utils.ts +98 -0
- package/src/color-picker/test/index.js +6 -15
- package/src/combobox-control/test/index.js +1 -6
- package/src/confirm-dialog/test/index.js +9 -29
- package/src/custom-gradient-picker/gradient-bar/utils.js +1 -1
- package/src/date-time/date/index.tsx +2 -1
- package/src/date-time/date/test/index.tsx +2 -8
- package/src/date-time/time/test/index.tsx +9 -29
- package/src/dimension-control/test/index.test.js +2 -8
- package/src/disabled/test/index.tsx +1 -5
- package/src/draggable/test/index.native.js +4 -4
- package/src/dropdown/README.md +1 -8
- package/src/dropdown/index.tsx +17 -6
- package/src/dropdown/stories/index.tsx +3 -3
- package/src/dropdown/test/index.tsx +2 -8
- package/src/dropdown/types.ts +9 -10
- package/src/dropdown-menu/README.md +1 -1
- package/src/dropdown-menu/stories/index.js +96 -27
- package/src/dropdown-menu/test/index.js +2 -8
- package/src/external-link/test/index.tsx +1 -6
- package/src/focal-point-picker/test/index.js +3 -11
- package/src/font-size-picker/test/index.tsx +14 -44
- package/src/form-file-upload/test/index.tsx +2 -17
- package/src/form-toggle/test/index.tsx +1 -5
- package/src/form-token-field/test/index.tsx +80 -163
- package/src/form-token-field/token.tsx +1 -1
- package/src/gradient-picker/index.js +15 -4
- package/src/guide/test/index.js +5 -17
- package/src/h-stack/component.tsx +0 -1
- package/src/higher-order/with-filters/test/index.js +24 -24
- package/src/higher-order/with-focus-outside/test/index.js +11 -25
- package/src/higher-order/with-focus-return/test/index.js +1 -5
- package/src/input-control/input-field.tsx +3 -1
- package/src/input-control/test/index.js +1 -6
- package/src/isolated-event-container/test/index.js +2 -8
- package/src/keyboard-shortcuts/README.md +1 -1
- package/src/keyboard-shortcuts/index.tsx +93 -0
- package/src/keyboard-shortcuts/stories/index.tsx +60 -0
- package/src/keyboard-shortcuts/test/{index.js → index.tsx} +16 -6
- package/src/keyboard-shortcuts/types.ts +51 -0
- package/src/modal/index.tsx +1 -2
- package/src/navigable-container/test/navigable-menu.js +5 -17
- package/src/navigable-container/test/tababble-container.js +3 -11
- package/src/navigation/test/index.js +3 -11
- package/src/navigator/test/index.tsx +6 -20
- package/src/notice/README.md +89 -42
- package/src/notice/{index.js → index.tsx} +28 -20
- package/src/notice/list.tsx +72 -0
- package/src/notice/stories/index.tsx +119 -0
- package/src/notice/test/__snapshots__/{index.js.snap → index.tsx.snap} +0 -0
- package/src/notice/test/{index.js → index.tsx} +7 -4
- package/src/notice/test/{list.js → list.tsx} +0 -0
- package/src/notice/types.ts +136 -0
- package/src/number-control/index.tsx +1 -1
- package/src/number-control/test/index.tsx +28 -86
- package/src/panel/test/body.js +2 -8
- package/src/placeholder/stories/index.tsx +1 -0
- package/src/query-controls/README.md +56 -56
- package/src/query-controls/author-select.tsx +37 -0
- package/src/query-controls/category-select.tsx +46 -0
- package/src/query-controls/index.tsx +192 -0
- package/src/query-controls/stories/index.tsx +205 -0
- package/src/query-controls/terms.ts +57 -0
- package/src/query-controls/test/{terms.js → terms.ts} +36 -20
- package/src/query-controls/types.ts +150 -0
- package/src/select-control/test/select-control.tsx +1 -6
- package/src/slot-fill/bubbles-virtually/fill.js +1 -0
- package/src/slot-fill/slot.js +1 -1
- package/src/slot-fill/use-slot.js +6 -16
- package/src/snackbar/index.tsx +6 -5
- package/src/snackbar/list.tsx +4 -2
- package/src/snackbar/types.ts +18 -92
- package/src/tab-panel/index.tsx +38 -16
- package/src/tab-panel/style.scss +8 -0
- package/src/tab-panel/test/index.tsx +35 -7
- package/src/tab-panel/types.ts +1 -1
- package/src/theme/test/index.tsx +4 -4
- package/src/toggle-group-control/stories/index.tsx +1 -0
- package/src/toggle-group-control/test/index.tsx +7 -23
- package/src/toolbar/stories/index.js +75 -72
- package/src/tools-panel/stories/index.js +3 -0
- package/src/tools-panel/test/index.js +1 -1
- package/src/tree-grid/index.js +1 -1
- package/src/tree-select/index.tsx +3 -6
- package/src/ui/context/test/context-connect.tsx +2 -0
- package/src/ui/context/test/wordpress-component.tsx +2 -0
- package/src/unit-control/test/index.tsx +21 -74
- package/src/utils/hooks/test/use-latest-ref.js +15 -18
- package/tsconfig.json +1 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/build-types/radio-context/index.d.ts +0 -6
- package/build-types/radio-context/index.d.ts.map +0 -1
- package/src/button/stories/index.js +0 -179
- package/src/keyboard-shortcuts/index.js +0 -56
- package/src/notice/list.js +0 -48
- package/src/notice/stories/index.js +0 -46
- package/src/query-controls/author-select.js +0 -23
- package/src/query-controls/category-select.js +0 -31
- package/src/query-controls/index.js +0 -122
- package/src/query-controls/terms.js +0 -40
- package/src/toolbar/stories/toolbar-button.js +0 -32
- package/src/toolbar/stories/toolbar-group.js +0 -33
|
@@ -12,8 +12,8 @@ import { plusCircle } from '@wordpress/icons';
|
|
|
12
12
|
/**
|
|
13
13
|
* Internal dependencies
|
|
14
14
|
*/
|
|
15
|
-
import Button from '
|
|
16
|
-
import
|
|
15
|
+
import Button from '..';
|
|
16
|
+
import Tooltip from '../../tooltip';
|
|
17
17
|
|
|
18
18
|
jest.mock( '../../icon', () => () => <div data-testid="test-icon" /> );
|
|
19
19
|
|
|
@@ -126,6 +126,7 @@ describe( 'Button', () => {
|
|
|
126
126
|
} );
|
|
127
127
|
|
|
128
128
|
it( 'should not pass the prop target into the element', () => {
|
|
129
|
+
// @ts-expect-error - `target` requires `href`
|
|
129
130
|
render( <Button target="_blank" /> );
|
|
130
131
|
|
|
131
132
|
expect( screen.getByRole( 'button' ) ).not.toHaveAttribute(
|
|
@@ -139,13 +140,12 @@ describe( 'Button', () => {
|
|
|
139
140
|
expect( screen.getByRole( 'button' ) ).toHaveClass( 'gutenberg' );
|
|
140
141
|
} );
|
|
141
142
|
|
|
142
|
-
it( 'should
|
|
143
|
-
render( <Button
|
|
143
|
+
it( 'should pass additional props to the element', () => {
|
|
144
|
+
render( <Button type="submit" /> );
|
|
144
145
|
|
|
145
|
-
expect( console ).toHaveErrored();
|
|
146
146
|
expect( screen.getByRole( 'button' ) ).toHaveAttribute(
|
|
147
|
-
'
|
|
148
|
-
'
|
|
147
|
+
'type',
|
|
148
|
+
'submit'
|
|
149
149
|
);
|
|
150
150
|
} );
|
|
151
151
|
|
|
@@ -342,6 +342,7 @@ describe( 'Button', () => {
|
|
|
342
342
|
} );
|
|
343
343
|
|
|
344
344
|
it( 'should become a button again when disabled is supplied', () => {
|
|
345
|
+
// @ts-expect-error - a button should not have `href`
|
|
345
346
|
render( <Button href="https://wordpress.org/" disabled /> );
|
|
346
347
|
|
|
347
348
|
expect( screen.getByRole( 'button' ) ).toBeVisible();
|
|
@@ -360,11 +361,13 @@ describe( 'Button', () => {
|
|
|
360
361
|
|
|
361
362
|
describe( 'deprecated props', () => {
|
|
362
363
|
it( 'should not break when the legacy isPrimary prop is passed', () => {
|
|
364
|
+
// @ts-expect-error
|
|
363
365
|
render( <Button isPrimary /> );
|
|
364
366
|
expect( screen.getByRole( 'button' ) ).toHaveClass( 'is-primary' );
|
|
365
367
|
} );
|
|
366
368
|
|
|
367
369
|
it( 'should not break when the legacy isSecondary prop is passed', () => {
|
|
370
|
+
// @ts-expect-error
|
|
368
371
|
render( <Button isSecondary /> );
|
|
369
372
|
expect( screen.getByRole( 'button' ) ).toHaveClass(
|
|
370
373
|
'is-secondary'
|
|
@@ -372,16 +375,19 @@ describe( 'Button', () => {
|
|
|
372
375
|
} );
|
|
373
376
|
|
|
374
377
|
it( 'should not break when the legacy isTertiary prop is passed', () => {
|
|
378
|
+
// @ts-expect-error
|
|
375
379
|
render( <Button isTertiary /> );
|
|
376
380
|
expect( screen.getByRole( 'button' ) ).toHaveClass( 'is-tertiary' );
|
|
377
381
|
} );
|
|
378
382
|
|
|
379
383
|
it( 'should not break when the legacy isLink prop is passed', () => {
|
|
384
|
+
// @ts-expect-error
|
|
380
385
|
render( <Button isLink /> );
|
|
381
386
|
expect( screen.getByRole( 'button' ) ).toHaveClass( 'is-link' );
|
|
382
387
|
} );
|
|
383
388
|
|
|
384
389
|
it( 'should warn when the isDefault prop is passed', () => {
|
|
390
|
+
// @ts-expect-error
|
|
385
391
|
render( <Button isDefault /> );
|
|
386
392
|
expect( screen.getByRole( 'button' ) ).toHaveClass(
|
|
387
393
|
'is-secondary'
|
|
@@ -389,4 +395,21 @@ describe( 'Button', () => {
|
|
|
389
395
|
expect( console ).toHaveWarned();
|
|
390
396
|
} );
|
|
391
397
|
} );
|
|
398
|
+
|
|
399
|
+
describe( 'static typing', () => {
|
|
400
|
+
<>
|
|
401
|
+
<Button href="foo" />
|
|
402
|
+
{ /* @ts-expect-error - `target` requires `href` */ }
|
|
403
|
+
<Button target="foo" />
|
|
404
|
+
{ /* @ts-expect-error - `disabled` is only for buttons */ }
|
|
405
|
+
<Button href="foo" disabled />
|
|
406
|
+
<Button href="foo" type="image/png" />
|
|
407
|
+
{ /* @ts-expect-error - if button, type must be submit/reset/button */ }
|
|
408
|
+
<Button type="image/png" />
|
|
409
|
+
{ /* @ts-expect-error */ }
|
|
410
|
+
<Button type="invalidtype" />
|
|
411
|
+
{ /* @ts-expect-error - although the runtime behavior will allow this to be an anchor, this is probably a mistake. */ }
|
|
412
|
+
<Button disabled __experimentalIsFocusable href="foo" />
|
|
413
|
+
</>;
|
|
414
|
+
} );
|
|
392
415
|
} );
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import type { Props as IconProps } from '../icon';
|
|
10
|
+
import type { PopoverProps } from '../popover/types';
|
|
11
|
+
import type { WordPressComponentProps } from '../ui/context/wordpress-component';
|
|
12
|
+
|
|
13
|
+
export type ButtonProps =
|
|
14
|
+
| WordPressComponentProps< BaseButtonProps & _ButtonProps, 'button', false >
|
|
15
|
+
| WordPressComponentProps< BaseButtonProps & AnchorProps, 'a', false >;
|
|
16
|
+
|
|
17
|
+
type BaseButtonProps = {
|
|
18
|
+
/**
|
|
19
|
+
* The button's children.
|
|
20
|
+
*/
|
|
21
|
+
children?: ReactNode;
|
|
22
|
+
/**
|
|
23
|
+
* An accessible description for the button.
|
|
24
|
+
*/
|
|
25
|
+
describedBy?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the button is focused.
|
|
28
|
+
*/
|
|
29
|
+
focus?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* If provided, renders an Icon component inside the button.
|
|
32
|
+
*/
|
|
33
|
+
icon?: IconProps< unknown >[ 'icon' ];
|
|
34
|
+
/**
|
|
35
|
+
* If provided with `icon`, sets the position of icon relative to the `text`.
|
|
36
|
+
*
|
|
37
|
+
* @default 'left'
|
|
38
|
+
*/
|
|
39
|
+
iconPosition?: 'left' | 'right';
|
|
40
|
+
/**
|
|
41
|
+
* If provided with `icon`, sets the icon size.
|
|
42
|
+
* Please refer to the Icon component for more details regarding
|
|
43
|
+
* the default value of its `size` prop.
|
|
44
|
+
*/
|
|
45
|
+
iconSize?: IconProps< unknown >[ 'size' ];
|
|
46
|
+
/**
|
|
47
|
+
* Indicates activity while a action is being performed.
|
|
48
|
+
*/
|
|
49
|
+
isBusy?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Renders a red text-based button style to indicate destructive behavior.
|
|
52
|
+
*/
|
|
53
|
+
isDestructive?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Renders a pressed button style.
|
|
56
|
+
*/
|
|
57
|
+
isPressed?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Decreases the size of the button.
|
|
60
|
+
*/
|
|
61
|
+
isSmall?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Sets the `aria-label` of the component, if none is provided.
|
|
64
|
+
* Sets the Tooltip content if `showTooltip` is provided.
|
|
65
|
+
*/
|
|
66
|
+
label?: string;
|
|
67
|
+
/**
|
|
68
|
+
* If provided with `showTooltip`, appends the Shortcut label to the tooltip content.
|
|
69
|
+
* If an object is provided, it should contain `display` and `ariaLabel` keys.
|
|
70
|
+
*/
|
|
71
|
+
shortcut?: string | { display: string; ariaLabel: string };
|
|
72
|
+
/**
|
|
73
|
+
* If provided, renders a Tooltip component for the button.
|
|
74
|
+
*/
|
|
75
|
+
showTooltip?: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* If provided, displays the given text inside the button. If the button contains children elements, the text is displayed before them.
|
|
78
|
+
*/
|
|
79
|
+
text?: string;
|
|
80
|
+
/**
|
|
81
|
+
* If provided with `showTooltip`, sets the position of the tooltip.
|
|
82
|
+
* Please refer to the Tooltip component for more details regarding the defaults.
|
|
83
|
+
*/
|
|
84
|
+
tooltipPosition?: PopoverProps[ 'position' ];
|
|
85
|
+
/**
|
|
86
|
+
* Specifies the button's style.
|
|
87
|
+
* The accepted values are:
|
|
88
|
+
* 'primary' (the primary button styles)
|
|
89
|
+
* 'secondary' (the default button styles)
|
|
90
|
+
* 'tertiary' (the text-based button styles)
|
|
91
|
+
* 'link' (the link button styles)
|
|
92
|
+
*/
|
|
93
|
+
variant?: 'primary' | 'secondary' | 'tertiary' | 'link';
|
|
94
|
+
/**
|
|
95
|
+
* Whether this is focusable.
|
|
96
|
+
*/
|
|
97
|
+
__experimentalIsFocusable?: boolean;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
type _ButtonProps = {
|
|
101
|
+
/**
|
|
102
|
+
* Whether the button is disabled.
|
|
103
|
+
* If `true`, this will force a `button` element to be rendered.
|
|
104
|
+
*/
|
|
105
|
+
disabled?: boolean;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
type AnchorProps = {
|
|
109
|
+
/**
|
|
110
|
+
* Whether the button is disabled.
|
|
111
|
+
* If `true`, this will force a `button` element to be rendered.
|
|
112
|
+
*/
|
|
113
|
+
disabled?: false;
|
|
114
|
+
/**
|
|
115
|
+
* If provided, renders `a` instead of `button`.
|
|
116
|
+
*/
|
|
117
|
+
href: string;
|
|
118
|
+
/**
|
|
119
|
+
* If provided with `href`, sets the `target` attribute to the `a`.
|
|
120
|
+
*/
|
|
121
|
+
target?: string;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export type DeprecatedButtonProps = {
|
|
125
|
+
isDefault?: boolean;
|
|
126
|
+
isLink?: boolean;
|
|
127
|
+
isPrimary?: boolean;
|
|
128
|
+
isSecondary?: boolean;
|
|
129
|
+
isTertiary?: boolean;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export type DeprecatedIconButtonProps = {
|
|
133
|
+
labelPosition: ButtonProps[ 'tooltipPosition' ];
|
|
134
|
+
showTooltip?: boolean;
|
|
135
|
+
size: ButtonProps[ 'iconSize' ];
|
|
136
|
+
label: ButtonProps[ 'label' ];
|
|
137
|
+
tooltip: ButtonProps[ 'label' ];
|
|
138
|
+
};
|
|
@@ -15,8 +15,6 @@ import { useState } from '@wordpress/element';
|
|
|
15
15
|
import BaseCheckboxControl from '..';
|
|
16
16
|
import type { CheckboxControlProps } from '../types';
|
|
17
17
|
|
|
18
|
-
jest.useFakeTimers();
|
|
19
|
-
|
|
20
18
|
const noop = () => {};
|
|
21
19
|
|
|
22
20
|
const getInput = () => screen.getByRole( 'checkbox' ) as HTMLInputElement;
|
|
@@ -85,9 +83,7 @@ describe( 'CheckboxControl', () => {
|
|
|
85
83
|
|
|
86
84
|
describe( 'Value', () => {
|
|
87
85
|
it( 'should flip the checked property when clicked', async () => {
|
|
88
|
-
const user = userEvent.setup(
|
|
89
|
-
advanceTimers: jest.advanceTimersByTime,
|
|
90
|
-
} );
|
|
86
|
+
const user = userEvent.setup();
|
|
91
87
|
|
|
92
88
|
let state = false;
|
|
93
89
|
const setState = jest.fn(
|
|
@@ -12,6 +12,7 @@ import ColorPalette from '../color-palette';
|
|
|
12
12
|
import ColorIndicator from '../color-indicator';
|
|
13
13
|
import Icon from '../icon';
|
|
14
14
|
import { HStack } from '../h-stack';
|
|
15
|
+
import type { ColorListPickerProps, ColorOptionProps } from './types';
|
|
15
16
|
|
|
16
17
|
function ColorOption( {
|
|
17
18
|
label,
|
|
@@ -20,7 +21,7 @@ function ColorOption( {
|
|
|
20
21
|
disableCustomColors,
|
|
21
22
|
enableAlpha,
|
|
22
23
|
onChange,
|
|
23
|
-
} ) {
|
|
24
|
+
}: ColorOptionProps ) {
|
|
24
25
|
const [ isOpen, setIsOpen ] = useState( false );
|
|
25
26
|
return (
|
|
26
27
|
<>
|
|
@@ -62,7 +63,7 @@ function ColorListPicker( {
|
|
|
62
63
|
disableCustomColors,
|
|
63
64
|
enableAlpha,
|
|
64
65
|
onChange,
|
|
65
|
-
} ) {
|
|
66
|
+
}: ColorListPickerProps ) {
|
|
66
67
|
return (
|
|
67
68
|
<div className="components-color-list-picker">
|
|
68
69
|
{ labels.map( ( label, index ) => (
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CSSProperties } from 'react';
|
|
5
|
+
|
|
6
|
+
export type ColorListPickerProps = {
|
|
7
|
+
/**
|
|
8
|
+
* A list of predifened colors. Each color is an object with a `name` and a
|
|
9
|
+
* `color` value.
|
|
10
|
+
* The `name` is a string used to identify the color in the UI.
|
|
11
|
+
* The `color` is a valid CSS color string.
|
|
12
|
+
*/
|
|
13
|
+
colors: Array< {
|
|
14
|
+
name: string;
|
|
15
|
+
color: NonNullable< CSSProperties[ 'color' ] >;
|
|
16
|
+
} >;
|
|
17
|
+
/**
|
|
18
|
+
* A list of labels for each of the options displayed in the UI.
|
|
19
|
+
*/
|
|
20
|
+
labels: Array< string >;
|
|
21
|
+
/**
|
|
22
|
+
* An array containing the currently selected colors.
|
|
23
|
+
*/
|
|
24
|
+
value?: Array< string | undefined >;
|
|
25
|
+
/**
|
|
26
|
+
* Controls whether the custom color picker is displayed.
|
|
27
|
+
*/
|
|
28
|
+
disableCustomColors?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Controls whether the ColorPalette should show an alpha channel control.
|
|
31
|
+
*/
|
|
32
|
+
enableAlpha?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* A function that receives the updated color value.
|
|
35
|
+
*/
|
|
36
|
+
onChange: ( newValue: Array< string | undefined > ) => void;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type ColorOptionProps = Pick<
|
|
40
|
+
ColorListPickerProps,
|
|
41
|
+
'colors' | 'disableCustomColors' | 'enableAlpha'
|
|
42
|
+
> & {
|
|
43
|
+
label: ColorListPickerProps[ 'labels' ][ number ];
|
|
44
|
+
value: string | undefined;
|
|
45
|
+
onChange: ( newValue: string | undefined ) => void;
|
|
46
|
+
};
|
|
@@ -43,7 +43,7 @@ Whether the palette should have a clearing button.
|
|
|
43
43
|
- Required: No
|
|
44
44
|
- Default: `true`
|
|
45
45
|
|
|
46
|
-
### `colors`: `
|
|
46
|
+
### `colors`: `PaletteObject[] | ColorObject[]`
|
|
47
47
|
|
|
48
48
|
Array with the colors to be shown. When displaying multiple color palettes to choose from, the format of the array changes from an array of colors objects, to an array of color palettes.
|
|
49
49
|
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
Platform,
|
|
12
12
|
Text,
|
|
13
13
|
} from 'react-native';
|
|
14
|
-
import { map } from 'lodash';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* WordPress dependencies
|
|
@@ -64,13 +63,21 @@ function ColorPalette( {
|
|
|
64
63
|
const opacity = useRef( new Animated.Value( 1 ) ).current;
|
|
65
64
|
|
|
66
65
|
const defaultColors = [
|
|
67
|
-
...new Set(
|
|
66
|
+
...new Set(
|
|
67
|
+
( defaultSettings.colors ?? [] ).map( ( { color } ) => color )
|
|
68
|
+
),
|
|
68
69
|
];
|
|
69
70
|
const mergedColors = [
|
|
70
|
-
...new Set(
|
|
71
|
+
...new Set(
|
|
72
|
+
( defaultSettings.allColors ?? [] ).map( ( { color } ) => color )
|
|
73
|
+
),
|
|
71
74
|
];
|
|
72
75
|
const defaultGradientColors = [
|
|
73
|
-
...new Set(
|
|
76
|
+
...new Set(
|
|
77
|
+
( defaultSettings.gradients ?? [] ).map(
|
|
78
|
+
( { gradient } ) => gradient
|
|
79
|
+
)
|
|
80
|
+
),
|
|
74
81
|
];
|
|
75
82
|
const colors = isGradientSegment ? defaultGradientColors : defaultColors;
|
|
76
83
|
|
|
@@ -10,7 +10,7 @@ import a11yPlugin from 'colord/plugins/a11y';
|
|
|
10
10
|
* WordPress dependencies
|
|
11
11
|
*/
|
|
12
12
|
import { __, sprintf } from '@wordpress/i18n';
|
|
13
|
-
import { useCallback, useMemo, forwardRef } from '@wordpress/element';
|
|
13
|
+
import { useCallback, useRef, useMemo, forwardRef } from '@wordpress/element';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Internal dependencies
|
|
@@ -33,6 +33,12 @@ import type {
|
|
|
33
33
|
} from './types';
|
|
34
34
|
import type { WordPressComponentProps } from '../ui/context';
|
|
35
35
|
import type { DropdownProps } from '../dropdown/types';
|
|
36
|
+
import {
|
|
37
|
+
extractColorNameFromCurrentValue,
|
|
38
|
+
isMultiplePaletteArray,
|
|
39
|
+
normalizeColorValue,
|
|
40
|
+
showTransparentBackground,
|
|
41
|
+
} from './utils';
|
|
36
42
|
|
|
37
43
|
extend( [ namesPlugin, a11yPlugin ] );
|
|
38
44
|
|
|
@@ -164,60 +170,11 @@ export function CustomColorPickerDropdown( {
|
|
|
164
170
|
);
|
|
165
171
|
}
|
|
166
172
|
|
|
167
|
-
export const extractColorNameFromCurrentValue = (
|
|
168
|
-
currentValue?: ColorPaletteProps[ 'value' ],
|
|
169
|
-
colors: ColorPaletteProps[ 'colors' ] = [],
|
|
170
|
-
showMultiplePalettes: boolean = false
|
|
171
|
-
) => {
|
|
172
|
-
if ( ! currentValue ) {
|
|
173
|
-
return '';
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const currentValueIsCssVariable = /^var\(/.test( currentValue );
|
|
177
|
-
const normalizedCurrentValue = currentValueIsCssVariable
|
|
178
|
-
? currentValue
|
|
179
|
-
: colord( currentValue ).toHex();
|
|
180
|
-
|
|
181
|
-
// Normalize format of `colors` to simplify the following loop
|
|
182
|
-
type normalizedPaletteObject = { colors: ColorObject[] };
|
|
183
|
-
const colorPalettes: normalizedPaletteObject[] = showMultiplePalettes
|
|
184
|
-
? ( colors as PaletteObject[] )
|
|
185
|
-
: [ { colors: colors as ColorObject[] } ];
|
|
186
|
-
for ( const { colors: paletteColors } of colorPalettes ) {
|
|
187
|
-
for ( const { name: colorName, color: colorValue } of paletteColors ) {
|
|
188
|
-
const normalizedColorValue = currentValueIsCssVariable
|
|
189
|
-
? colorValue
|
|
190
|
-
: colord( colorValue ).toHex();
|
|
191
|
-
|
|
192
|
-
if ( normalizedCurrentValue === normalizedColorValue ) {
|
|
193
|
-
return colorName;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// translators: shown when the user has picked a custom color (i.e not in the palette of colors).
|
|
199
|
-
return __( 'Custom' );
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
export const showTransparentBackground = ( currentValue?: string ) => {
|
|
203
|
-
if ( typeof currentValue === 'undefined' ) {
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
return colord( currentValue ).alpha() === 0;
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
const areColorsMultiplePalette = (
|
|
210
|
-
colors: NonNullable< ColorPaletteProps[ 'colors' ] >
|
|
211
|
-
): colors is PaletteObject[] => {
|
|
212
|
-
return colors.every( ( colorObj ) =>
|
|
213
|
-
Array.isArray( ( colorObj as PaletteObject ).colors )
|
|
214
|
-
);
|
|
215
|
-
};
|
|
216
|
-
|
|
217
173
|
function UnforwardedColorPalette(
|
|
218
174
|
props: WordPressComponentProps< ColorPaletteProps, 'div' >,
|
|
219
175
|
forwardedRef: ForwardedRef< any >
|
|
220
176
|
) {
|
|
177
|
+
const customColorPaletteRef = useRef< HTMLElement | null >( null );
|
|
221
178
|
const {
|
|
222
179
|
clearable = true,
|
|
223
180
|
colors = [],
|
|
@@ -230,9 +187,7 @@ function UnforwardedColorPalette(
|
|
|
230
187
|
} = props;
|
|
231
188
|
const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );
|
|
232
189
|
|
|
233
|
-
const hasMultipleColorOrigins =
|
|
234
|
-
colors.length > 0 &&
|
|
235
|
-
( colors as PaletteObject[] )[ 0 ].colors !== undefined;
|
|
190
|
+
const hasMultipleColorOrigins = isMultiplePaletteArray( colors );
|
|
236
191
|
const buttonLabelName = useMemo(
|
|
237
192
|
() =>
|
|
238
193
|
extractColorNameFromCurrentValue(
|
|
@@ -243,22 +198,10 @@ function UnforwardedColorPalette(
|
|
|
243
198
|
[ value, colors, hasMultipleColorOrigins ]
|
|
244
199
|
);
|
|
245
200
|
|
|
246
|
-
// Make sure that the `colors` array has a valid format.
|
|
247
|
-
if (
|
|
248
|
-
colors.length > 0 &&
|
|
249
|
-
hasMultipleColorOrigins !== areColorsMultiplePalette( colors )
|
|
250
|
-
) {
|
|
251
|
-
// eslint-disable-next-line no-console
|
|
252
|
-
console.warn(
|
|
253
|
-
'wp.components.ColorPalette: please specify a valid format for the `colors` prop. '
|
|
254
|
-
);
|
|
255
|
-
return null;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
201
|
const renderCustomColorPicker = () => (
|
|
259
202
|
<DropdownContentWrapper paddingSize="none">
|
|
260
203
|
<ColorPicker
|
|
261
|
-
color={ value }
|
|
204
|
+
color={ normalizeColorValue( value, customColorPaletteRef ) }
|
|
262
205
|
onChange={ ( color ) => onChange( color ) }
|
|
263
206
|
enableAlpha={ enableAlpha }
|
|
264
207
|
/>
|
|
@@ -303,6 +246,7 @@ function UnforwardedColorPalette(
|
|
|
303
246
|
renderToggle={ ( { isOpen, onToggle } ) => (
|
|
304
247
|
<Flex
|
|
305
248
|
as={ 'button' }
|
|
249
|
+
ref={ customColorPaletteRef }
|
|
306
250
|
justify="space-between"
|
|
307
251
|
align="flex-start"
|
|
308
252
|
className="components-color-palette__custom-color"
|
|
@@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
|
|
|
9
9
|
*/
|
|
10
10
|
import ColorPalette from '..';
|
|
11
11
|
|
|
12
|
-
jest.useFakeTimers();
|
|
13
|
-
|
|
14
12
|
const EXAMPLE_COLORS = [
|
|
15
13
|
{ name: 'red', color: '#f00' },
|
|
16
14
|
{ name: 'green', color: '#0f0' },
|
|
@@ -54,9 +52,7 @@ describe( 'ColorPalette', () => {
|
|
|
54
52
|
} );
|
|
55
53
|
|
|
56
54
|
it( 'should call onClick on an active button with undefined', async () => {
|
|
57
|
-
const user = userEvent.setup(
|
|
58
|
-
advanceTimers: jest.advanceTimersByTime,
|
|
59
|
-
} );
|
|
55
|
+
const user = userEvent.setup();
|
|
60
56
|
const onChange = jest.fn();
|
|
61
57
|
|
|
62
58
|
render(
|
|
@@ -76,9 +72,7 @@ describe( 'ColorPalette', () => {
|
|
|
76
72
|
} );
|
|
77
73
|
|
|
78
74
|
it( 'should call onClick on an inactive button', async () => {
|
|
79
|
-
const user = userEvent.setup(
|
|
80
|
-
advanceTimers: jest.advanceTimersByTime,
|
|
81
|
-
} );
|
|
75
|
+
const user = userEvent.setup();
|
|
82
76
|
const onChange = jest.fn();
|
|
83
77
|
|
|
84
78
|
render(
|
|
@@ -104,9 +98,7 @@ describe( 'ColorPalette', () => {
|
|
|
104
98
|
} );
|
|
105
99
|
|
|
106
100
|
it( 'should call onClick with undefined, when the clearButton onClick is triggered', async () => {
|
|
107
|
-
const user = userEvent.setup(
|
|
108
|
-
advanceTimers: jest.advanceTimersByTime,
|
|
109
|
-
} );
|
|
101
|
+
const user = userEvent.setup();
|
|
110
102
|
const onChange = jest.fn();
|
|
111
103
|
|
|
112
104
|
render(
|
|
@@ -139,9 +131,7 @@ describe( 'ColorPalette', () => {
|
|
|
139
131
|
} );
|
|
140
132
|
|
|
141
133
|
it( 'should render dropdown and its content', async () => {
|
|
142
|
-
const user = userEvent.setup(
|
|
143
|
-
advanceTimers: jest.advanceTimersByTime,
|
|
144
|
-
} );
|
|
134
|
+
const user = userEvent.setup();
|
|
145
135
|
const onChange = jest.fn();
|
|
146
136
|
|
|
147
137
|
render(
|
|
@@ -55,7 +55,7 @@ export type ColorPaletteProps = Pick< PaletteProps, 'onChange' > & {
|
|
|
55
55
|
*
|
|
56
56
|
* @default []
|
|
57
57
|
*/
|
|
58
|
-
colors?:
|
|
58
|
+
colors?: PaletteObject[] | ColorObject[];
|
|
59
59
|
/**
|
|
60
60
|
* Whether to allow the user to pick a custom color on top of the predefined
|
|
61
61
|
* choices (defined via the `colors` prop).
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { RefObject } from 'react';
|
|
5
|
+
import { colord, extend } from 'colord';
|
|
6
|
+
import namesPlugin from 'colord/plugins/names';
|
|
7
|
+
import a11yPlugin from 'colord/plugins/a11y';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* WordPress dependencies
|
|
11
|
+
*/
|
|
12
|
+
import { __ } from '@wordpress/i18n';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Internal dependencies
|
|
16
|
+
*/
|
|
17
|
+
import type { ColorObject, ColorPaletteProps, PaletteObject } from './types';
|
|
18
|
+
|
|
19
|
+
extend( [ namesPlugin, a11yPlugin ] );
|
|
20
|
+
|
|
21
|
+
export const extractColorNameFromCurrentValue = (
|
|
22
|
+
currentValue?: ColorPaletteProps[ 'value' ],
|
|
23
|
+
colors: ColorPaletteProps[ 'colors' ] = [],
|
|
24
|
+
showMultiplePalettes: boolean = false
|
|
25
|
+
) => {
|
|
26
|
+
if ( ! currentValue ) {
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const currentValueIsCssVariable = /^var\(/.test( currentValue );
|
|
31
|
+
const normalizedCurrentValue = currentValueIsCssVariable
|
|
32
|
+
? currentValue
|
|
33
|
+
: colord( currentValue ).toHex();
|
|
34
|
+
|
|
35
|
+
// Normalize format of `colors` to simplify the following loop
|
|
36
|
+
type normalizedPaletteObject = { colors: ColorObject[] };
|
|
37
|
+
const colorPalettes: normalizedPaletteObject[] = showMultiplePalettes
|
|
38
|
+
? ( colors as PaletteObject[] )
|
|
39
|
+
: [ { colors: colors as ColorObject[] } ];
|
|
40
|
+
for ( const { colors: paletteColors } of colorPalettes ) {
|
|
41
|
+
for ( const { name: colorName, color: colorValue } of paletteColors ) {
|
|
42
|
+
const normalizedColorValue = currentValueIsCssVariable
|
|
43
|
+
? colorValue
|
|
44
|
+
: colord( colorValue ).toHex();
|
|
45
|
+
|
|
46
|
+
if ( normalizedCurrentValue === normalizedColorValue ) {
|
|
47
|
+
return colorName;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// translators: shown when the user has picked a custom color (i.e not in the palette of colors).
|
|
53
|
+
return __( 'Custom' );
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const showTransparentBackground = ( currentValue?: string ) => {
|
|
57
|
+
if ( typeof currentValue === 'undefined' ) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
return colord( currentValue ).alpha() === 0;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// The PaletteObject type has a `colors` property (an array of ColorObject),
|
|
64
|
+
// while the ColorObject type has a `color` property (the CSS color value).
|
|
65
|
+
export const isMultiplePaletteObject = (
|
|
66
|
+
obj: PaletteObject | ColorObject
|
|
67
|
+
): obj is PaletteObject =>
|
|
68
|
+
Array.isArray( ( obj as PaletteObject ).colors ) && ! ( 'color' in obj );
|
|
69
|
+
|
|
70
|
+
export const isMultiplePaletteArray = (
|
|
71
|
+
arr: ( PaletteObject | ColorObject )[]
|
|
72
|
+
): arr is PaletteObject[] => {
|
|
73
|
+
return (
|
|
74
|
+
arr.length > 0 &&
|
|
75
|
+
arr.every( ( colorObj ) => isMultiplePaletteObject( colorObj ) )
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const normalizeColorValue = (
|
|
80
|
+
value: string | undefined,
|
|
81
|
+
ref: RefObject< HTMLElement > | null
|
|
82
|
+
) => {
|
|
83
|
+
const currentValueIsCssVariable = /^var\(/.test( value ?? '' );
|
|
84
|
+
|
|
85
|
+
if ( ! currentValueIsCssVariable || ! ref?.current ) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const { ownerDocument } = ref.current;
|
|
90
|
+
const { defaultView } = ownerDocument;
|
|
91
|
+
const computedBackgroundColor = defaultView?.getComputedStyle(
|
|
92
|
+
ref.current
|
|
93
|
+
).backgroundColor;
|
|
94
|
+
|
|
95
|
+
return computedBackgroundColor
|
|
96
|
+
? colord( computedBackgroundColor ).toHex()
|
|
97
|
+
: value;
|
|
98
|
+
};
|