diff --git a/docs/source/blocks/block-style-wrapper.md b/docs/source/blocks/block-style-wrapper.md index e3e825207e..b4cc1ece45 100644 --- a/docs/source/blocks/block-style-wrapper.md +++ b/docs/source/blocks/block-style-wrapper.md @@ -11,6 +11,9 @@ myst: # Block style wrapper +```{versionadded} 16.0.0 +``` + The block style wrapper is part of a block anatomy. It allows you to inject styles from the block schema into the block wrapper in the form of class names. It wraps the block edit and the view components. @@ -109,7 +112,7 @@ config.blocks.blocksConfig.listing.schemaEnhancer = composeSchema( The `styles` field is mapped to an `objectWidget`. The following is an example of a possible set of styles. -The style wrapper will read the styles and inject them into the edit and view components as shown in the next sections. +The style wrapper will read the styles and inject the resultant class names into the edit and view components as shown in the next sections. ```json { @@ -127,7 +130,7 @@ The style wrapper will read the styles and inject them into the edit and view co ## Using the injected class names in your block The resultant class names are injected as a `className` prop into the wrapped block. -Thus you can use it in the root component of your block view and edit components as follows: +Thus you can use it in the root component of your block view component as follows: ```jsx const BlockView = (props)=> ( @@ -149,6 +152,8 @@ The resultant HTML would be the following: ``` Then it's at your discretion how you define the CSS class names in your theme. +## Customize the injected class names + If you need other style of classnames generated, you can use the classname converters defined in `config.settings.styleClassNameConverters`, by registering fieldnames suffixed with the converter name. For example, a style @@ -165,6 +170,72 @@ data like: will generate classnames `primary inverted` +## Inject custom CSS properties + +```{versionadded} 17.8.0 +``` + +The style wrapper also allows to inject custom CSS properties. +This is useful where the property that you want to inject is customizable per project. +For example, imagine you have an existing global CSS custom property `--image-aspect-ratio` that you use for all images in all blocks. +Then in your customized theme, you could set CSS attributes for this property, changing it in runtime. +The key feature of custom CSS properties is that they can be applied also using the cascade. +This means that they can be placed anywhere in either CSS definitions or HTML structure, and they will be applied only in the context where they are defined. + +As an example, first define the style of a teaser block image as follows. + +```css +.block.teaser img { + aspect-ratio: var(--image-aspect-ratio, 16 / 9); +} +``` + +Next, you can enhance a block's schema by injecting the custom CSS property as follows. + +```js + schema.properties.styles.schema.fieldsets[0].fields = [ + ...schema.properties.styles.schema.fieldsets[0].fields, + '--image-aspect-ratio', + ]; + + // We use a select widget and set a default + schema.properties.styles.schema.properties['--image-aspect-ratio'] = { + widget: 'select', + title: 'Aspect Ratio', + choices: [ + ['1', '1:1'], + ['16 / 9', '16/9'], + ], + default: '1', + }; +``` + +Finally, assuming that you select the default value for the {guilabel}`Aspect Ratio` for the custom CSS property, then the markup of the block will contain the custom property as shown. + +```html +
+``` + +The custom CSS property definition will only apply within the div that it's defined. +As you can see, the custom CSS property applies only within the `div` in which it is defined. + +If you want to use it in your custom components, you need to apply it in the root of your block's view component as follows: + +```jsx +const BlockView = (props)=> ( +