Skip to content

Commit

Permalink
add box-shadow support for blocks via theme.json (#41972)
Browse files Browse the repository at this point in the history
* add box-shadow support to blocks in theme.json

reduce specificity for default button shadow

update schema for shadow

fix lint issues

* add shadow support in style-engine

* add condition to handle the case of shadow style property
  • Loading branch information
madhusudhand authored Aug 25, 2022
1 parent 3fdfda3 commit 53da761
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ CSS and SVG filter styles.
| --- | --- |--- |
| duotone | string | |

---

### shadow

Box shadow styles.


---

<!-- END TOKEN Autogenerated - DO NOT EDIT -->
10 changes: 7 additions & 3 deletions lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class WP_Theme_JSON_6_1 extends WP_Theme_JSON_6_0 {
'text-decoration' => array( 'typography', 'textDecoration' ),
'text-transform' => array( 'typography', 'textTransform' ),
'filter' => array( 'filter', 'duotone' ),
'box-shadow' => array( 'shadow' ),
);

/**
Expand Down Expand Up @@ -153,9 +154,11 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
// hence, the schema for blocks & elements should not have them.
$styles_non_top_level = static::VALID_STYLES;
foreach ( array_keys( $styles_non_top_level ) as $section ) {
foreach ( array_keys( $styles_non_top_level[ $section ] ) as $prop ) {
if ( 'top' === $styles_non_top_level[ $section ][ $prop ] ) {
unset( $styles_non_top_level[ $section ][ $prop ] );
if ( array_key_exists( $section, $styles_non_top_level ) && is_array( $styles_non_top_level[ $section ] ) ) {
foreach ( array_keys( $styles_non_top_level[ $section ] ) as $prop ) {
if ( 'top' === $styles_non_top_level[ $section ][ $prop ] ) {
unset( $styles_non_top_level[ $section ][ $prop ] );
}
}
}
}
Expand Down Expand Up @@ -314,6 +317,7 @@ public static function remove_insecure_properties( $theme_json ) {
'gradient' => null,
'text' => null,
),
'shadow' => null,
'filter' => array(
'duotone' => null,
),
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/button/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ $blocks-block__margin: 0.5em;
// Prefer the link selector instead of the regular button classname
// to support the previous markup in addition to the new one.
.wp-block-button__link {
box-shadow: none;
cursor: pointer;
display: inline-block;
text-align: center;
Expand All @@ -25,6 +24,7 @@ $blocks-block__margin: 0.5em;
// They are needed for backwards compatibility.
:where(.wp-block-button__link) {
// This needs a low specificity so it won't override the rules from the button element if defined in theme.json.
box-shadow: none;
text-decoration: none;

// 100% causes an oval, but any explicit but really high value retains the pill shape.
Expand Down
2 changes: 2 additions & 0 deletions packages/style-engine/src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import border from './border';
import color from './color';
import shadow from './shadow';
import spacing from './spacing';
import typography from './typography';

Expand All @@ -11,4 +12,5 @@ export const styleDefinitions = [
...color,
...spacing,
...typography,
...shadow,
];
14 changes: 14 additions & 0 deletions packages/style-engine/src/styles/shadow/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../../types';
import { generateRule } from '../utils';

const shadow = {
name: 'shadow',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule( style, options, [ 'shadow' ], 'boxShadow' );
},
};

export default [ shadow ];
8 changes: 7 additions & 1 deletion schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@
}
},
"additionalProperties": false
},
"shadow": {
"description": "Box shadow styles.",
"type": "string"
}
}
},
Expand All @@ -1101,7 +1105,9 @@
"border": {},
"color": {},
"spacing": {},
"typography": {}
"typography": {},
"filter": {},
"shadow": {}
},
"additionalProperties": false
}
Expand Down

0 comments on commit 53da761

Please sign in to comment.