-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
block-editor
, remove t…
…he external re-export from `@wordpress-components` and start replacing app consumers to import it from `block-editor` Start with `hasSplitBorders`
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { CSSProperties } from 'react'; | ||
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Check
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / All
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Run performance tests
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 1
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 2
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 3
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 4
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 5
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 6
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 7
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Playwright - 8
Check failure on line 4 in packages/block-editor/src/components/border-box-utils/index.ts GitHub Actions / Build JavaScript assets for PHP unit tests
|
||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { | ||
Border, | ||
AnyBorder, | ||
Borders, | ||
BorderProp, | ||
BorderSide, | ||
} from './types'; | ||
|
||
const sides: BorderSide[] = [ 'top', 'right', 'bottom', 'left' ]; | ||
const borderProps: BorderProp[] = [ 'color', 'style', 'width' ]; | ||
|
||
const isEmptyBorder = ( border?: Border ) => { | ||
if ( ! border ) { | ||
return true; | ||
} | ||
return ! borderProps.some( ( prop ) => border[ prop ] !== undefined ); | ||
}; | ||
|
||
export const isDefinedBorder = ( border: AnyBorder ) => { | ||
// No border, no worries :) | ||
if ( ! border ) { | ||
return false; | ||
} | ||
|
||
// If we have individual borders per side within the border object we | ||
// need to check whether any of those side borders have been set. | ||
if ( hasSplitBorders( border ) ) { | ||
const allSidesEmpty = sides.every( ( side ) => | ||
isEmptyBorder( ( border as Borders )[ side ] ) | ||
); | ||
|
||
return ! allSidesEmpty; | ||
} | ||
|
||
// If we have a top-level border only, check if that is empty. e.g. | ||
// { color: undefined, style: undefined, width: undefined } | ||
// Border radius can still be set within the border object as it is | ||
// handled separately. | ||
return ! isEmptyBorder( border as Border ); | ||
}; | ||
|
||
export const hasSplitBorders = ( border: AnyBorder = {} ) => { | ||
return Object.keys( border ).some( | ||
( side ) => sides.indexOf( side as BorderSide ) !== -1 | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { CSSProperties } from 'react'; | ||
|
||
export type Border = { | ||
color?: CSSProperties[ 'borderColor' ]; | ||
style?: CSSProperties[ 'borderStyle' ]; | ||
width?: CSSProperties[ 'borderWidth' ]; | ||
}; | ||
|
||
export type Borders = { | ||
top?: Border; | ||
right?: Border; | ||
bottom?: Border; | ||
left?: Border; | ||
}; | ||
|
||
export type AnyBorder = Border | Borders | undefined; | ||
export type BorderProp = keyof Border; | ||
export type BorderSide = keyof Borders; |