Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't hardcode CSS units #32482

Merged
merged 6 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/block-editor/src/hooks/border-radius.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import {
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
parseUnit,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { parseUnit } from './border';
import { cleanEmptyObject } from './utils';
import useSetting from '../components/use-setting';

Expand All @@ -32,7 +32,8 @@ export function BorderRadiusEdit( props ) {

// Step value is maintained in state so step is appropriate for current unit
// even when current radius value is undefined.
const initialStep = parseUnit( style?.border?.radius ) === 'px' ? 1 : 0.25;
const initialStep =
parseUnit( style?.border?.radius )[ 1 ] === 'px' ? 1 : 0.25;
const [ step, setStep ] = useState( initialStep );

const onUnitChange = ( newUnit ) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/block-editor/src/hooks/border-width.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import {
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
parseUnit,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { parseUnit } from './border';
import { cleanEmptyObject } from './utils';
import useSetting from '../components/use-setting';

Expand All @@ -32,7 +32,8 @@ export const BorderWidthEdit = ( props ) => {

// Step value is maintained in state so step is appropriate for current unit
// even when current radius value is undefined.
const initialStep = parseUnit( style?.border?.width ) === 'px' ? 1 : 0.25;
const initialStep =
parseUnit( style?.border?.width )[ 1 ] === 'px' ? 1 : 0.25;
const [ step, setStep ] = useState( initialStep );

const onUnitChange = ( newUnit ) => {
Expand Down
17 changes: 1 addition & 16 deletions packages/block-editor/src/hooks/border.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { getBlockSupport } from '@wordpress/blocks';
import { PanelBody, ALL_CSS_UNITS } from '@wordpress/components';
import { PanelBody } from '@wordpress/components';
import { Platform } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

Expand All @@ -18,21 +18,6 @@ import { BorderWidthEdit } from './border-width';

export const BORDER_SUPPORT_KEY = '__experimentalBorder';

/**
* Parses a CSS unit from a border CSS value.
*
* @param {string} cssValue CSS value to parse e.g. `10px` or `1.5em`.
* @return {string} CSS unit from provided value or default 'px'.
*/
export function parseUnit( cssValue ) {
const value = String( cssValue ).trim();
const unitMatch = value.match( /[\d.\-\+]*\s*(.*)/ )[ 1 ];
const unit = unitMatch !== undefined ? unitMatch.toLowerCase() : '';
const currentUnit = ALL_CSS_UNITS.find( ( item ) => item.value === unit );

return currentUnit?.value || 'px';
}

export function BorderPanel( props ) {
const isDisabled = useIsBorderDisabled( props );
const isSupported = hasBorderSupport( props.name );
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export { Truncate as __experimentalTruncate } from './truncate';
export {
default as __experimentalUnitControl,
useCustomUnits as __experimentalUseCustomUnits,
ALL_CSS_UNITS,
parseUnit,
aristath marked this conversation as resolved.
Show resolved Hide resolved
} from './unit-control';
export { default as VisuallyHidden } from './visually-hidden';
export { VStack as __experimentalVStack } from './v-stack';
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/unit-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,5 @@ function UnitControl(

const ForwardedUnitControl = forwardRef( UnitControl );

export { ALL_CSS_UNITS, useCustomUnits } from './utils';
export { parseUnit, useCustomUnits } from './utils';
export default ForwardedUnitControl;