@wordpress/components 29.6.0 → 29.8.0

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/build/angle-picker-control/index.js +1 -1
  3. package/build/angle-picker-control/index.js.map +1 -1
  4. package/build/autocomplete/index.js +5 -30
  5. package/build/autocomplete/index.js.map +1 -1
  6. package/build/custom-select-control-v2/styles.js +9 -9
  7. package/build/custom-select-control-v2/styles.js.map +1 -1
  8. package/build/number-control/index.js +6 -4
  9. package/build/number-control/index.js.map +1 -1
  10. package/build/popover/index.js +29 -17
  11. package/build/popover/index.js.map +1 -1
  12. package/build/popover/types.js.map +1 -1
  13. package/build/range-control/index.js +6 -5
  14. package/build/range-control/index.js.map +1 -1
  15. package/build/toolbar/toolbar-dropdown-menu/index.js +4 -3
  16. package/build/toolbar/toolbar-dropdown-menu/index.js.map +1 -1
  17. package/build/toolbar/toolbar-item/index.js +4 -3
  18. package/build/toolbar/toolbar-item/index.js.map +1 -1
  19. package/build/utils/get-node-text.js +30 -0
  20. package/build/utils/get-node-text.js.map +1 -0
  21. package/build/utils/math.js +18 -15
  22. package/build/utils/math.js.map +1 -1
  23. package/build-module/angle-picker-control/index.js +1 -1
  24. package/build-module/angle-picker-control/index.js.map +1 -1
  25. package/build-module/autocomplete/index.js +3 -28
  26. package/build-module/autocomplete/index.js.map +1 -1
  27. package/build-module/custom-select-control-v2/styles.js +9 -9
  28. package/build-module/custom-select-control-v2/styles.js.map +1 -1
  29. package/build-module/number-control/index.js +7 -5
  30. package/build-module/number-control/index.js.map +1 -1
  31. package/build-module/popover/index.js +28 -16
  32. package/build-module/popover/index.js.map +1 -1
  33. package/build-module/popover/types.js.map +1 -1
  34. package/build-module/range-control/index.js +6 -5
  35. package/build-module/range-control/index.js.map +1 -1
  36. package/build-module/toolbar/toolbar-dropdown-menu/index.js +3 -2
  37. package/build-module/toolbar/toolbar-dropdown-menu/index.js.map +1 -1
  38. package/build-module/toolbar/toolbar-item/index.js +3 -2
  39. package/build-module/toolbar/toolbar-item/index.js.map +1 -1
  40. package/build-module/utils/get-node-text.js +24 -0
  41. package/build-module/utils/get-node-text.js.map +1 -0
  42. package/build-module/utils/math.js +17 -14
  43. package/build-module/utils/math.js.map +1 -1
  44. package/build-style/style-rtl.css +15 -13
  45. package/build-style/style.css +15 -13
  46. package/build-types/angle-picker-control/index.d.ts +1 -1
  47. package/build-types/autocomplete/index.d.ts +2 -2
  48. package/build-types/autocomplete/index.d.ts.map +1 -1
  49. package/build-types/custom-select-control-v2/styles.d.ts.map +1 -1
  50. package/build-types/number-control/index.d.ts.map +1 -1
  51. package/build-types/popover/index.d.ts +18 -2
  52. package/build-types/popover/index.d.ts.map +1 -1
  53. package/build-types/popover/stories/e2e/index.story.d.ts +8 -1
  54. package/build-types/popover/stories/e2e/index.story.d.ts.map +1 -1
  55. package/build-types/popover/stories/index.story.d.ts +1 -0
  56. package/build-types/popover/stories/index.story.d.ts.map +1 -1
  57. package/build-types/popover/test/utils/index.d.ts.map +1 -1
  58. package/build-types/popover/types.d.ts +9 -0
  59. package/build-types/popover/types.d.ts.map +1 -1
  60. package/build-types/range-control/index.d.ts +6 -5
  61. package/build-types/range-control/index.d.ts.map +1 -1
  62. package/build-types/toolbar/toolbar-dropdown-menu/index.d.ts +2 -2
  63. package/build-types/toolbar/toolbar-dropdown-menu/index.d.ts.map +1 -1
  64. package/build-types/toolbar/toolbar-item/index.d.ts +2 -2
  65. package/build-types/toolbar/toolbar-item/index.d.ts.map +1 -1
  66. package/build-types/utils/get-node-text.d.ts +3 -0
  67. package/build-types/utils/get-node-text.d.ts.map +1 -0
  68. package/build-types/utils/math.d.ts +10 -11
  69. package/build-types/utils/math.d.ts.map +1 -1
  70. package/package.json +20 -20
  71. package/src/angle-picker-control/README.md +1 -1
  72. package/src/angle-picker-control/index.tsx +1 -1
  73. package/src/autocomplete/index.tsx +3 -30
  74. package/src/button/style.scss +1 -1
  75. package/src/combobox-control/style.scss +4 -2
  76. package/src/custom-select-control-v2/styles.ts +1 -0
  77. package/src/form-token-field/style.scss +6 -6
  78. package/src/number-control/index.tsx +8 -7
  79. package/src/number-control/test/index.tsx +15 -2
  80. package/src/popover/index.tsx +34 -20
  81. package/src/popover/stories/index.story.tsx +83 -2
  82. package/src/popover/test/utils/index.tsx +0 -1
  83. package/src/popover/types.ts +10 -0
  84. package/src/range-control/index.tsx +6 -5
  85. package/src/toolbar/toolbar-dropdown-menu/index.tsx +3 -2
  86. package/src/toolbar/toolbar-item/index.tsx +3 -2
  87. package/src/utils/get-node-text.ts +24 -0
  88. package/src/utils/math.js +17 -22
  89. package/src/utils/test/get-node-text.js +37 -0
  90. package/src/utils/test/math.js +28 -32
  91. package/src/utils/theme-variables.scss +0 -4
  92. package/tsconfig.tsbuildinfo +1 -1
package/src/utils/math.js CHANGED
@@ -59,9 +59,9 @@ function getPrecision( value ) {
59
59
  /**
60
60
  * Clamps a value based on a min/max range.
61
61
  *
62
- * @param {number} value The value.
63
- * @param {number} min The minimum range.
64
- * @param {number} max The maximum range.
62
+ * @param {number|string} value The value.
63
+ * @param {number} min The minimum range.
64
+ * @param {number} max The maximum range.
65
65
  *
66
66
  * @return {number} The clamped value.
67
67
  */
@@ -71,28 +71,23 @@ export function clamp( value, min, max ) {
71
71
  }
72
72
 
73
73
  /**
74
- * Clamps a value based on a min/max range with rounding
74
+ * Rounds a value to the nearest step offset by a minimum.
75
75
  *
76
- * @param {number | string} value The value.
77
- * @param {number} min The minimum range.
78
- * @param {number} max The maximum range.
79
- * @param {number} step A multiplier for the value.
76
+ * @param {number|string} value The value.
77
+ * @param {number} min The minimum range.
78
+ * @param {number} step The increment for the value.
80
79
  *
81
- * @return {number} The rounded and clamped value.
80
+ * @return {number} The value as a valid step.
82
81
  */
83
- export function roundClamp(
84
- value = 0,
85
- min = Infinity,
86
- max = Infinity,
87
- step = 1
88
- ) {
82
+ export function ensureValidStep( value, min, step ) {
89
83
  const baseValue = getNumber( value );
90
84
  const stepValue = getNumber( step );
91
- const precision = getPrecision( step );
92
- const rounded = Math.round( baseValue / stepValue ) * stepValue;
93
- const clampedValue = clamp( rounded, min, max );
94
-
95
- return precision
96
- ? getNumber( clampedValue.toFixed( precision ) )
97
- : clampedValue;
85
+ const precision = Math.max( getPrecision( step ), getPrecision( min ) );
86
+ const realMin = Math.abs( min ) === Infinity ? 0 : min;
87
+ // If the step is not a factor of the minimum then the step must be
88
+ // offset by the minimum.
89
+ const tare = realMin % stepValue ? realMin : 0;
90
+ const rounded = Math.round( ( baseValue - tare ) / stepValue ) * stepValue;
91
+ const fromMin = rounded + tare;
92
+ return precision ? getNumber( fromMin.toFixed( precision ) ) : fromMin;
98
93
  }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import getNodeText from '../get-node-text';
5
+
6
+ describe( 'getNodeText', () => {
7
+ it( 'should return an empty string for null', () => {
8
+ expect( getNodeText( null ) ).toBe( '' );
9
+ } );
10
+
11
+ it( 'should return the string representation of a string node', () => {
12
+ expect( getNodeText( 'Hello' ) ).toBe( 'Hello' );
13
+ } );
14
+
15
+ it( 'should return the string representation of a number node', () => {
16
+ expect( getNodeText( 123 ) ).toBe( '123' );
17
+ } );
18
+
19
+ it( 'should return an empty string for a boolean node', () => {
20
+ expect( getNodeText( true ) ).toBe( '' );
21
+ } );
22
+
23
+ it( 'should concatenate text from an array of nodes', () => {
24
+ expect( getNodeText( [ 'Hello', ' ', 'World' ] ) ).toBe(
25
+ 'Hello World'
26
+ );
27
+ } );
28
+
29
+ it( 'should return text from a React element with children', () => {
30
+ const element = (
31
+ <div>
32
+ Hello <span>World</span>
33
+ </div>
34
+ );
35
+ expect( getNodeText( element ) ).toBe( 'Hello World' );
36
+ } );
37
+ } );
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { add, clamp, subtract, roundClamp } from '../math';
4
+
5
+ import { add, subtract, clamp, ensureValidStep } from '../math';
5
6
 
6
7
  describe( 'add', () => {
7
8
  it( 'should add string and number values', () => {
@@ -58,44 +59,39 @@ describe( 'clamp', () => {
58
59
  } );
59
60
  } );
60
61
 
61
- describe( 'roundClamp', () => {
62
- it( 'should clamp a value between min and max', () => {
63
- expect( roundClamp( 10, 1, 10 ) ).toBe( 10 );
64
- expect( roundClamp( 1, 1, 10 ) ).toBe( 1 );
65
- expect( roundClamp( 0, 1, 10 ) ).toBe( 1 );
62
+ describe( 'ensureValidStep', () => {
63
+ it( 'should work with number or string values', () => {
64
+ expect( ensureValidStep( '49', 0, 10 ) ).toBe( 50 );
65
+ expect( ensureValidStep( 49, '0', 10 ) ).toBe( 50 );
66
+ expect( ensureValidStep( 49, 0, '10' ) ).toBe( 50 );
67
+ } );
66
68
 
67
- expect( roundClamp( 50, 1, 10 ) ).toBe( 10 );
68
- expect( roundClamp( 50, -10, 10 ) ).toBe( 10 );
69
- expect( roundClamp( -50, -10, 10 ) ).toBe( -10 );
69
+ it( 'should round to step', () => {
70
+ expect( ensureValidStep( 40, 0, 10 ) ).toBe( 40 );
71
+ expect( ensureValidStep( 42, 0, 10 ) ).toBe( 40 );
72
+ expect( ensureValidStep( 45, 0, 10 ) ).toBe( 50 );
73
+ expect( ensureValidStep( 49, 0, 10 ) ).toBe( 50 );
70
74
 
71
- expect( roundClamp( '50', 1, 10 ) ).toBe( 10 );
72
- expect( roundClamp( '50', -10, 10 ) ).toBe( 10 );
73
- expect( roundClamp( -50, -10, '10' ) ).toBe( -10 );
75
+ expect( ensureValidStep( 50, 0, 15 ) ).toBe( 45 );
76
+ expect( ensureValidStep( 50, 0, 11 ) ).toBe( 55 );
74
77
  } );
75
78
 
76
- it( 'should clamp number or string values', () => {
77
- expect( roundClamp( '50', 1, 10 ) ).toBe( 10 );
78
- expect( roundClamp( '50', -10, 10 ) ).toBe( 10 );
79
- expect( roundClamp( -50, -10, '10' ) ).toBe( -10 );
79
+ it( 'should round with float in step', () => {
80
+ expect( ensureValidStep( 40.5, 1, 0.1 ) ).toBe( 40.5 );
81
+ expect( ensureValidStep( 40.05, 1, 0.01 ) ).toBe( 40.05 );
82
+ expect( ensureValidStep( 40.06, 1, 0.1 ) ).toBe( 40.1 );
83
+ expect( ensureValidStep( 40.123005, 1, 0.001 ) ).toBe( 40.123 );
80
84
  } );
81
85
 
82
- it( 'should clamp with step', () => {
83
- expect( roundClamp( 40, 1, 100, 10 ) ).toBe( 40 );
84
- expect( roundClamp( 42, 1, 100, 10 ) ).toBe( 40 );
85
- expect( roundClamp( 45, 1, 100, 10 ) ).toBe( 50 );
86
- expect( roundClamp( 49, 1, 100, 10 ) ).toBe( 50 );
87
- expect( roundClamp( 50, 1, 100, 10 ) ).toBe( 50 );
88
-
89
- expect( roundClamp( 50, 1, 100, 15 ) ).toBe( 45 );
90
- expect( roundClamp( 50, 1, 100, '15' ) ).toBe( 45 );
91
- expect( roundClamp( 50, 1, 100, 11 ) ).toBe( 55 );
86
+ it( 'should round to steps starting from min', () => {
87
+ expect( ensureValidStep( 10, 0.25, 1 ) ).toBe( 10.25 );
88
+ expect( ensureValidStep( 10, -20.25, 1 ) ).toBe( 9.75 );
89
+ expect( ensureValidStep( 10.5, 0.05, 0.1 ) ).toBe( 10.45 );
90
+ expect( ensureValidStep( 10.51, 0.05, 0.1 ) ).toBe( 10.55 );
92
91
  } );
93
92
 
94
- it( 'should clamp with float in step', () => {
95
- expect( roundClamp( 40, 1, 100, 1 ) ).toBe( 40 );
96
- expect( roundClamp( 40.5, 1, 100, 0.1 ) ).toBe( 40.5 );
97
- expect( roundClamp( 40.05, 1, 100, 0.01 ) ).toBe( 40.05 );
98
- expect( roundClamp( 40.06, 1, 100, 0.1 ) ).toBe( 40.1 );
99
- expect( roundClamp( 40.123005, 1, 100, 0.001 ) ).toBe( 40.123 );
93
+ it( 'should round with a precision that’s the greater of min and step', () => {
94
+ expect( ensureValidStep( 10.061, 0.01, 0.1 ) ).toBe( 10.11 );
95
+ expect( ensureValidStep( 10.105, 0.1, 0.01 ) ).toBe( 10.11 );
100
96
  } );
101
97
  } );
@@ -4,10 +4,6 @@
4
4
  // `--wp-admin-theme-color`. If the `--wp-admin-theme-color` variable is not
5
5
  // defined, fallback to the default theme color (WP blueberry).
6
6
  $components-color-accent: var(--wp-components-color-accent, var(--wp-admin-theme-color, #3858e9));
7
-
8
- // Define accent color transparent variants.
9
- $components-color-accent-transparent-40: rgba(var(--wp-components-color-accent--rgb, var(--wp-admin-theme-color--rgb)), 0.04);
10
-
11
7
  $components-color-accent-darker-10: var(--wp-components-color-accent-darker-10, var(--wp-admin-theme-color-darker-10, #2145e6));
12
8
  $components-color-accent-darker-20: var(--wp-components-color-accent-darker-20, var(--wp-admin-theme-color-darker-20, #183ad6));
13
9