Skip to content

Commit

Permalink
Move getSpacingPresetCssVar to outermost utils folder
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Jan 21, 2025
1 parent 88882f4 commit aaedb08
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export { __experimentalImageURLInputUI } from './url-popover/image-url-input-ui'
export { default as withColorContext } from './color-palette/with-color-context';
export { default as __experimentalSpacingSizesControl } from './spacing-sizes-control';
export {
getSpacingPresetCssVar,
isValueSpacingPreset,
getCustomValueFromPreset,
} from './spacing-sizes-control/utils';
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export { default as VideoPlayer, VIDEO_ASPECT_RATIO } from './video-player';
export { default as AudioPlayer } from './audio-player';

export {
getSpacingPresetCssVar,
getCustomValueFromPreset,
isValueSpacingPreset,
} from './spacing-sizes-control/utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getInitialView,
getPresetValueFromCustomValue,
getSliderValueFromPreset,
getSpacingPresetCssVar,
getSpacingPresetSlug,
hasAxisSupport,
hasBalancedSidesSupport,
Expand Down Expand Up @@ -73,17 +72,6 @@ describe( 'getPresetValueFromCustomValue', () => {
} );
} );

describe( 'getSpacingPresetCssVar', () => {
it( 'should return original value if not a string in spacing presets var format', () => {
expect( getSpacingPresetCssVar( '20px' ) ).toBe( '20px' );
} );
it( 'should return a string in spacing presets css var format with slug from provided value', () => {
expect( getSpacingPresetCssVar( 'var:preset|spacing|20' ) ).toBe(
'var(--wp--preset--spacing--20)'
);
} );
} );

describe( 'getSpacingPresetSlug', () => {
it( 'should return original value if 0 or default', () => {
expect( getSpacingPresetSlug( '0' ) ).toBe( '0' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,6 @@ export function getPresetValueFromCustomValue( value, spacingSizes ) {
return value;
}

/**
* Converts a spacing preset into a custom value.
*
* @param {string} value Value to convert.
*
* @return {string | undefined} CSS var string for given spacing preset value.
*/
export function getSpacingPresetCssVar( value ) {
if ( ! value ) {
return;
}

const slug = value.match( /var:preset\|spacing\|(.+)/ );

if ( ! slug ) {
return value;
}

return `var(--wp--preset--spacing--${ slug[ 1 ] })`;
}

/**
* Returns the slug section of the given spacing preset string.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/gap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { getSpacingPresetCssVar } from '../components/spacing-sizes-control/utils';
import { getSpacingPresetCssVar } from '../utils/get-spacing-preset-css-var';

/**
* Returns a BoxControl object value from a given blockGap style value.
Expand Down
20 changes: 20 additions & 0 deletions packages/block-editor/src/utils/get-spacing-preset-css-var.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Converts a spacing preset into a custom value.
*
* @param {string} value Value to convert.
*
* @return {string | undefined} CSS var string for given spacing preset value.
*/
export function getSpacingPresetCssVar( value ) {
if ( ! value ) {
return;
}

const slug = value.match( /var:preset\|spacing\|(.+)/ );

if ( ! slug ) {
return value;
}

return `var(--wp--preset--spacing--${ slug[ 1 ] })`;
}
1 change: 1 addition & 0 deletions packages/block-editor/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as transformStyles } from './transform-styles';
export { default as getPxFromCssUnit } from './get-px-from-css-unit';
export { useBlockBindingsUtils } from './block-bindings';
export { getSpacingPresetCssVar } from './get-spacing-preset-css-var';
15 changes: 15 additions & 0 deletions packages/block-editor/src/utils/test/get-spacing-preset-css-var.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Internal dependencies
*/
import { getSpacingPresetCssVar } from '../get-spacing-preset-css-var';

describe( 'getSpacingPresetCssVar', () => {
it( 'should return original value if not a string in spacing presets var format', () => {
expect( getSpacingPresetCssVar( '20px' ) ).toBe( '20px' );
} );
it( 'should return a string in spacing presets css var format with slug from provided value', () => {
expect( getSpacingPresetCssVar( 'var:preset|spacing|20' ) ).toBe(
'var(--wp--preset--spacing--20)'
);
} );
} );

0 comments on commit aaedb08

Please sign in to comment.