-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy border box style helpers to a module in
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
1 parent
9c5897f
commit a4af26d
Showing
10 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/block-editor/src/components/border-box-utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { CSSProperties } from 'react'; | ||
|
||
/** | ||
* 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 | ||
); | ||
}; |
21 changes: 21 additions & 0 deletions
21
packages/block-editor/src/components/border-box-utils/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters