@wordpress/components 29.5.1 → 29.7.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 (100) hide show
  1. package/CHANGELOG.md +21 -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/font-size-picker/font-size-picker-select.js +1 -8
  9. package/build/font-size-picker/font-size-picker-select.js.map +1 -1
  10. package/build/font-size-picker/index.js +12 -34
  11. package/build/font-size-picker/index.js.map +1 -1
  12. package/build/font-size-picker/styles.js +12 -19
  13. package/build/font-size-picker/styles.js.map +1 -1
  14. package/build/font-size-picker/utils.js +0 -22
  15. package/build/font-size-picker/utils.js.map +1 -1
  16. package/build/number-control/index.js +6 -4
  17. package/build/number-control/index.js.map +1 -1
  18. package/build/range-control/index.js +6 -5
  19. package/build/range-control/index.js.map +1 -1
  20. package/build/toolbar/toolbar-dropdown-menu/index.js +4 -3
  21. package/build/toolbar/toolbar-dropdown-menu/index.js.map +1 -1
  22. package/build/toolbar/toolbar-item/index.js +4 -3
  23. package/build/toolbar/toolbar-item/index.js.map +1 -1
  24. package/build/utils/get-node-text.js +30 -0
  25. package/build/utils/get-node-text.js.map +1 -0
  26. package/build/utils/math.js +18 -15
  27. package/build/utils/math.js.map +1 -1
  28. package/build-module/angle-picker-control/index.js +1 -1
  29. package/build-module/angle-picker-control/index.js.map +1 -1
  30. package/build-module/autocomplete/index.js +3 -28
  31. package/build-module/autocomplete/index.js.map +1 -1
  32. package/build-module/custom-select-control-v2/styles.js +9 -9
  33. package/build-module/custom-select-control-v2/styles.js.map +1 -1
  34. package/build-module/font-size-picker/font-size-picker-select.js +2 -9
  35. package/build-module/font-size-picker/font-size-picker-select.js.map +1 -1
  36. package/build-module/font-size-picker/index.js +14 -36
  37. package/build-module/font-size-picker/index.js.map +1 -1
  38. package/build-module/font-size-picker/styles.js +11 -18
  39. package/build-module/font-size-picker/styles.js.map +1 -1
  40. package/build-module/font-size-picker/utils.js +0 -22
  41. package/build-module/font-size-picker/utils.js.map +1 -1
  42. package/build-module/number-control/index.js +7 -5
  43. package/build-module/number-control/index.js.map +1 -1
  44. package/build-module/range-control/index.js +6 -5
  45. package/build-module/range-control/index.js.map +1 -1
  46. package/build-module/toolbar/toolbar-dropdown-menu/index.js +3 -2
  47. package/build-module/toolbar/toolbar-dropdown-menu/index.js.map +1 -1
  48. package/build-module/toolbar/toolbar-item/index.js +3 -2
  49. package/build-module/toolbar/toolbar-item/index.js.map +1 -1
  50. package/build-module/utils/get-node-text.js +24 -0
  51. package/build-module/utils/get-node-text.js.map +1 -0
  52. package/build-module/utils/math.js +17 -14
  53. package/build-module/utils/math.js.map +1 -1
  54. package/build-style/style-rtl.css +1 -3
  55. package/build-style/style.css +1 -3
  56. package/build-types/angle-picker-control/index.d.ts +1 -1
  57. package/build-types/autocomplete/index.d.ts +2 -2
  58. package/build-types/autocomplete/index.d.ts.map +1 -1
  59. package/build-types/custom-select-control-v2/styles.d.ts.map +1 -1
  60. package/build-types/font-size-picker/font-size-picker-select.d.ts.map +1 -1
  61. package/build-types/font-size-picker/index.d.ts.map +1 -1
  62. package/build-types/font-size-picker/styles.d.ts +0 -4
  63. package/build-types/font-size-picker/styles.d.ts.map +1 -1
  64. package/build-types/font-size-picker/utils.d.ts +1 -9
  65. package/build-types/font-size-picker/utils.d.ts.map +1 -1
  66. package/build-types/number-control/index.d.ts.map +1 -1
  67. package/build-types/range-control/index.d.ts +6 -5
  68. package/build-types/range-control/index.d.ts.map +1 -1
  69. package/build-types/toolbar/toolbar-dropdown-menu/index.d.ts +2 -2
  70. package/build-types/toolbar/toolbar-dropdown-menu/index.d.ts.map +1 -1
  71. package/build-types/toolbar/toolbar-item/index.d.ts +2 -2
  72. package/build-types/toolbar/toolbar-item/index.d.ts.map +1 -1
  73. package/build-types/utils/get-node-text.d.ts +3 -0
  74. package/build-types/utils/get-node-text.d.ts.map +1 -0
  75. package/build-types/utils/math.d.ts +10 -11
  76. package/build-types/utils/math.d.ts.map +1 -1
  77. package/package.json +19 -19
  78. package/src/angle-picker-control/README.md +1 -1
  79. package/src/angle-picker-control/index.tsx +1 -1
  80. package/src/autocomplete/index.tsx +3 -30
  81. package/src/button/style.scss +0 -2
  82. package/src/custom-select-control-v2/styles.ts +1 -0
  83. package/src/font-size-picker/font-size-picker-select.tsx +2 -12
  84. package/src/font-size-picker/index.tsx +18 -47
  85. package/src/font-size-picker/styles.ts +0 -5
  86. package/src/font-size-picker/test/index.tsx +36 -80
  87. package/src/font-size-picker/test/utils.ts +1 -37
  88. package/src/font-size-picker/utils.ts +1 -24
  89. package/src/form-token-field/style.scss +1 -1
  90. package/src/number-control/index.tsx +8 -7
  91. package/src/number-control/test/index.tsx +15 -2
  92. package/src/range-control/index.tsx +6 -5
  93. package/src/toolbar/toolbar-dropdown-menu/index.tsx +3 -2
  94. package/src/toolbar/toolbar-item/index.tsx +3 -2
  95. package/src/utils/get-node-text.ts +24 -0
  96. package/src/utils/math.js +17 -22
  97. package/src/utils/test/get-node-text.js +37 -0
  98. package/src/utils/test/math.js +28 -32
  99. package/src/utils/theme-variables.scss +0 -4
  100. package/tsconfig.tsbuildinfo +1 -1
@@ -40,8 +40,21 @@ describe( 'NumberControl', () => {
40
40
  } );
41
41
 
42
42
  it( 'should render custom className', () => {
43
- render( <NumberControl className="hello" /> );
44
- expect( screen.getByRole( 'spinbutton' ) ).toBeVisible();
43
+ const { container: withoutClassName } = render( <NumberControl /> );
44
+
45
+ const { container: withClassName } = render(
46
+ <NumberControl className="hello" />
47
+ );
48
+
49
+ expect(
50
+ // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
51
+ withoutClassName.querySelector( '.components-number-control' )
52
+ ).not.toHaveClass( 'hello' );
53
+
54
+ expect(
55
+ // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
56
+ withClassName.querySelector( '.components-number-control' )
57
+ ).toHaveClass( 'hello' );
45
58
  } );
46
59
  } );
47
60
 
@@ -391,17 +391,18 @@ function UnforwardedRangeControl(
391
391
  * import { useState } from '@wordpress/element';
392
392
  *
393
393
  * const MyRangeControl = () => {
394
- * const [ isChecked, setChecked ] = useState( true );
394
+ * const [ value, setValue ] = useState();
395
395
  * return (
396
396
  * <RangeControl
397
397
  * __nextHasNoMarginBottom
398
398
  * __next40pxDefaultSize
399
399
  * help="Please select how transparent you would like this."
400
- * initialPosition={50}
400
+ * initialPosition={ 50 }
401
401
  * label="Opacity"
402
- * max={100}
403
- * min={0}
404
- * onChange={() => {}}
402
+ * max={ 100 }
403
+ * min={ 0 }
404
+ * value={ value }
405
+ * onChange={ setValue }
405
406
  * />
406
407
  * );
407
408
  * };
@@ -16,7 +16,7 @@ import ToolbarContext from '../toolbar-context';
16
16
  import DropdownMenu from '../../dropdown-menu';
17
17
  import type { DropdownMenuProps } from '../../dropdown-menu/types';
18
18
 
19
- function ToolbarDropdownMenu(
19
+ function UnforwardedToolbarDropdownMenu(
20
20
  props: DropdownMenuProps,
21
21
  ref: ForwardedRef< any >
22
22
  ) {
@@ -44,4 +44,5 @@ function ToolbarDropdownMenu(
44
44
  );
45
45
  }
46
46
 
47
- export default forwardRef( ToolbarDropdownMenu );
47
+ export const ToolbarDropdownMenu = forwardRef( UnforwardedToolbarDropdownMenu );
48
+ export default ToolbarDropdownMenu;
@@ -16,7 +16,7 @@ import warning from '@wordpress/warning';
16
16
  import ToolbarContext from '../toolbar-context';
17
17
  import type { ToolbarItemProps } from './types';
18
18
 
19
- function ToolbarItem(
19
+ function UnforwardedToolbarItem(
20
20
  { children, as: Component, ...props }: ToolbarItemProps,
21
21
  ref: ForwardedRef< any >
22
22
  ) {
@@ -57,4 +57,5 @@ function ToolbarItem(
57
57
  );
58
58
  }
59
59
 
60
- export default forwardRef( ToolbarItem );
60
+ export const ToolbarItem = forwardRef( UnforwardedToolbarItem );
61
+ export default ToolbarItem;
@@ -0,0 +1,24 @@
1
+ const getNodeText = ( node: React.ReactNode ): string => {
2
+ if ( node === null ) {
3
+ return '';
4
+ }
5
+
6
+ switch ( typeof node ) {
7
+ case 'string':
8
+ case 'number':
9
+ return node.toString();
10
+ case 'object': {
11
+ if ( node instanceof Array ) {
12
+ return node.map( getNodeText ).join( '' );
13
+ }
14
+ if ( 'props' in node ) {
15
+ return getNodeText( node.props.children );
16
+ }
17
+ return '';
18
+ }
19
+ default:
20
+ return '';
21
+ }
22
+ };
23
+
24
+ export default getNodeText;
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