-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: getInlineStyles + tests * Feat: Add getTypographyStyles() * Feat: getBackgroundStyles() * Feat: getTextStyles() * Feat: getBorderStyles() * Refactor: Use getCSSRules instead of compileCSS * Refactor: Improve BlockWithAttributes type * Refactor: Fix types for BlockWithAttributes * Test: getStyles() * Chore: Fix WordPressBlock type * Chore: Changeset
- Loading branch information
Showing
21 changed files
with
388 additions
and
8 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@faustwp/blocks': patch | ||
--- | ||
|
||
Adds getStyles helper that generates inline styles from block attributes. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ThemeJson } from '../theme.js'; | ||
import { BlockWithAttributes } from '../components/WordPressBlocksViewer.js'; | ||
|
||
/** | ||
* | ||
* Returns the specified block Background Styles | ||
* @param theme Block Theme object | ||
* @returns React CSS Properties object | ||
*/ | ||
export default function getBackgroundStyles<T extends BlockWithAttributes>( | ||
theme: ThemeJson, | ||
block: T, | ||
): React.CSSProperties { | ||
const { attributes } = block; | ||
const styles: React.CSSProperties = {}; | ||
if (attributes?.backgroundColor) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
styles.backgroundColor = `var(--wp--preset--color--${attributes.backgroundColor})`; | ||
} | ||
return styles; | ||
} |
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 @@ | ||
import { ThemeJson } from '../theme.js'; | ||
import { BlockWithAttributes } from '../components/WordPressBlocksViewer.js'; | ||
|
||
/** | ||
* | ||
* Returns the specified block Border Styles | ||
* @param theme Block Theme object | ||
* @returns React CSS Properties object | ||
*/ | ||
export default function getBorderStyles<T extends BlockWithAttributes>( | ||
theme: ThemeJson, | ||
block: T, | ||
): React.CSSProperties { | ||
const { attributes } = block; | ||
const styles: React.CSSProperties = {}; | ||
if (attributes?.borderColor) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
styles.borderColor = `var(--wp--preset--color--${attributes.borderColor})`; | ||
} | ||
return styles; | ||
} |
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,34 @@ | ||
import { getCSSRules } from '@wordpress/style-engine'; | ||
|
||
type Style = Parameters<typeof getCSSRules>[0]; | ||
|
||
export function parseInlineStyles(styles: Style): React.CSSProperties { | ||
const output: Record<string, unknown> = {}; | ||
getCSSRules(styles).forEach((rule) => { | ||
output[rule.key] = rule.value; | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
/** | ||
* Parses and returns any inline css styles on this block attributes | ||
* @param attributes Block Attributes object | ||
* @returns React CSS Properties object | ||
*/ | ||
export default function getInlineStyles( | ||
attributes: Record<string, unknown>, | ||
): React.CSSProperties | undefined { | ||
let styles; | ||
// Duck Typing | ||
if (attributes?.style) { | ||
try { | ||
styles = parseInlineStyles( | ||
JSON.parse(attributes?.style as string) as Style, | ||
); | ||
} catch (e) { | ||
return styles; | ||
} | ||
} | ||
return styles; | ||
} |
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,20 @@ | ||
import { ThemeJson } from '../theme.js'; | ||
import { BlockWithAttributes } from '../components/WordPressBlocksViewer.js'; | ||
import getInlineStyles from './getInlineStyles.js'; | ||
import getTypographyStyles from './getTypographyStyles.js'; | ||
import getBackgroundStyles from './getBackgroundStyles.js'; | ||
import getTextStyles from './getTextStyles.js'; | ||
import getBorderStyles from './getBorderStyles.js'; | ||
|
||
export default function getStyles<T extends BlockWithAttributes>( | ||
theme: ThemeJson, | ||
block: T, | ||
): React.CSSProperties { | ||
return { | ||
...getInlineStyles(block?.attributes ?? {}), | ||
...getTypographyStyles(theme, block), | ||
...getBackgroundStyles(theme, block), | ||
...getTextStyles(theme, block), | ||
...getBorderStyles(theme, block), | ||
}; | ||
} |
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 @@ | ||
import { ThemeJson } from '../theme.js'; | ||
import { BlockWithAttributes } from '../components/WordPressBlocksViewer.js'; | ||
|
||
/** | ||
* | ||
* Returns the specified block Text Styles | ||
* @param theme Block Theme object | ||
* @returns React CSS Properties object | ||
*/ | ||
export default function getTextStyles<T extends BlockWithAttributes>( | ||
theme: ThemeJson, | ||
block: T, | ||
): React.CSSProperties { | ||
const { attributes } = block; | ||
const styles: React.CSSProperties = {}; | ||
if (attributes?.textColor) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
styles.color = `var(--wp--preset--color--${attributes.textColor})`; | ||
} | ||
return styles; | ||
} |
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,28 @@ | ||
import { ThemeJson } from '../theme.js'; | ||
import { BlockWithAttributes } from '../components/WordPressBlocksViewer.js'; | ||
|
||
/** | ||
* | ||
* Returns the specified block Typography Styles | ||
* @param theme Block Theme object | ||
* @returns React CSS Properties object | ||
*/ | ||
export default function getTypographyStyles<T extends BlockWithAttributes>( | ||
theme: ThemeJson, | ||
block: T, | ||
): React.CSSProperties { | ||
const { attributes } = block; | ||
const styles: React.CSSProperties = {}; | ||
if (attributes?.fontFamily) { | ||
// TODO: Have a CSS Var generator strategy generate those. | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
styles.fontFamily = `var(--wp--preset--font-family--${attributes.fontFamily})`; | ||
} | ||
|
||
if (attributes?.fontSize) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
styles.fontSize = `var(--wp--preset--font-size--${attributes.fontSize})`; | ||
} | ||
|
||
return styles; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { BlockWithAttributes } from '../../src/components/WordPressBlocksViewer.js'; | ||
import { ThemeJson } from '../../src/theme.js'; | ||
import getBackgroundStyles from '../../src/utils/getBackgroundStyles.js'; | ||
|
||
describe('getBackgroundStyles()', () => { | ||
const theme = {}; | ||
it.each([ | ||
[theme, { attributes: {} }, {}], | ||
[ | ||
theme, | ||
{ | ||
attributes: { | ||
backgroundColor: 'primary', | ||
}, | ||
}, | ||
{ | ||
backgroundColor: 'var(--wp--preset--color--primary)', | ||
}, | ||
], | ||
])( | ||
'theme %p and block %p expecting background Styles %p', | ||
( | ||
theme: ThemeJson, | ||
block: BlockWithAttributes, | ||
result: React.CSSProperties | undefined, | ||
) => { | ||
expect(getBackgroundStyles(theme, block)).toEqual(result); | ||
}, | ||
); | ||
}); |
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,30 @@ | ||
import { BlockWithAttributes } from '../../src/components/WordPressBlocksViewer.js'; | ||
import { ThemeJson } from '../../src/theme.js'; | ||
import getBorderStyles from '../../src/utils/getBorderStyles.js'; | ||
|
||
describe('getBorderStyles()', () => { | ||
const theme = {}; | ||
it.each([ | ||
[theme, { attributes: {} }, {}], | ||
[ | ||
theme, | ||
{ | ||
attributes: { | ||
borderColor: 'primary', | ||
}, | ||
}, | ||
{ | ||
borderColor: 'var(--wp--preset--color--primary)', | ||
}, | ||
], | ||
])( | ||
'theme %p and block %p expecting border Styles %p', | ||
( | ||
theme: ThemeJson, | ||
block: BlockWithAttributes, | ||
result: React.CSSProperties | undefined, | ||
) => { | ||
expect(getBorderStyles(theme, block)).toEqual(result); | ||
}, | ||
); | ||
}); |
Oops, something went wrong.
66ecbb3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check out the recent updates to your Atlas environment:
Learn more about building on Atlas in our documentation.