Skip to content

Commit

Permalink
First commit -just getting things working for the top level styles (b…
Browse files Browse the repository at this point in the history
…ody tag)

Style engine
Toggle control that only appears on global styles
  • Loading branch information
ramonjd committed Jun 7, 2024
1 parent 3bd6799 commit 2bdb93b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function gutenberg_render_background_support( $block_content, $block ) {
}

$styles = gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );

var_dump( $block_attributes['style']['background'] );

Check failure on line 70 in lib/block-supports/background.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 1 tabs, found 0
if ( ! empty( $styles['css'] ) ) {
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
$tags = new WP_HTML_Tag_Processor( $block_content );
Expand Down
10 changes: 6 additions & 4 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class WP_Theme_JSON_Gutenberg {
'background-position' => array( 'background', 'backgroundPosition' ),
'background-repeat' => array( 'background', 'backgroundRepeat' ),
'background-size' => array( 'background', 'backgroundSize' ),
'background-attachment' => array( 'background', 'backgroundAttachment' ),
'border-radius' => array( 'border', 'radius' ),
'border-top-left-radius' => array( 'border', 'radius', 'topLeft' ),
'border-top-right-radius' => array( 'border', 'radius', 'topRight' ),
Expand Down Expand Up @@ -502,10 +503,11 @@ class WP_Theme_JSON_Gutenberg {
*/
const VALID_STYLES = array(
'background' => array(
'backgroundImage' => 'top',
'backgroundPosition' => 'top',
'backgroundRepeat' => 'top',
'backgroundSize' => 'top',
'backgroundImage' => 'top',
'backgroundAttachment' => 'top',
'backgroundPosition' => 'top',
'backgroundRepeat' => 'top',
'backgroundSize' => 'top',
),
'border' => array(
'color' => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function BackgroundImageToolsPanelItem( {
style,
inheritedValue,
themeFileURIs,
settings,
} ) {
const mediaUpload = useSelect(
( select ) => select( blockEditorStore ).getSettings().mediaUpload,
Expand All @@ -205,6 +206,13 @@ function BackgroundImageToolsPanelItem( {
...inheritedValue?.background?.backgroundImage,
};

const shouldShowBackgroundAttachmentControls =
settings?.background?.backgroundAttachment;

const attachmentValue = style?.background?.backgroundAttachment || {
...inheritedValue?.background?.backgroundAttachment,
};

const replaceContainerRef = useRef();

const { createErrorNotice } = useDispatch( noticesStore );
Expand Down Expand Up @@ -289,6 +297,15 @@ function BackgroundImageToolsPanelItem( {
};
}, [] );

const toggleScrollWithPage = () =>
onChange(
setImmutably(
style,
[ 'background', 'backgroundAttachment' ],
attachmentValue === 'fixed' ? 'scroll' : 'fixed'
)
);

const hasValue = hasBackgroundImageValue( style );

const closeAndFocus = () => {
Expand All @@ -309,7 +326,9 @@ function BackgroundImageToolsPanelItem( {
const canRemove = ! hasValue && hasBackgroundImageValue( inheritedValue );

return (
<ToolsPanelItem
<VStack
as={ ToolsPanelItem }
spacing={ 4 }
className="single-column"
hasValue={ () => hasValue }
label={ __( 'Background image' ) }
Expand Down Expand Up @@ -366,7 +385,19 @@ function BackgroundImageToolsPanelItem( {
label={ __( 'Drop to upload' ) }
/>
</div>
</ToolsPanelItem>
{ shouldShowBackgroundAttachmentControls && (
<div className="block-editor-global-styles-background-panel__attachment">
<ToggleControl
label={ __( 'Scroll with page' ) }
checked={ attachmentValue !== 'fixed' }
onChange={ toggleScrollWithPage }
help={ __(
'Whether your image should scroll with the page or stay fixed in place.'
) }
/>
</div>
) }
</VStack>
);
}

Expand Down Expand Up @@ -646,6 +677,7 @@ export default function BackgroundPanel( {
style={ value }
inheritedValue={ inheritedValue }
themeFileURIs={ themeFileURIs }
settings={ settings }
/>
{ shouldShowBackgroundSizeControls && (
<BackgroundSizeToolsPanelItem
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export function BackgroundImagePanel( {
...settings,
background: {
...settings.background,
backgroundAttachment: false,
backgroundSize:
settings?.background?.backgroundSize &&
hasBackgroundSupport( name, 'backgroundSize' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export default function BackgroundPanel() {
hasBackgroundImageValue( inheritedStyle ),
};

const updatedSettings = {
...settings,
background: {
...settings.background,
backgroundAttachment: true,
},
};

return (
<StylesBackgroundPanel
inheritedValue={ inheritedStyle }
Expand Down
14 changes: 10 additions & 4 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,32 @@ final class WP_Style_Engine {
*/
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
'background' => array(
'backgroundImage' => array(
'backgroundImage' => array(
'property_keys' => array(
'default' => 'background-image',
),
'value_func' => array( self::class, 'get_url_or_value_css_declaration' ),
'path' => array( 'background', 'backgroundImage' ),
),
'backgroundPosition' => array(
'backgroundAttachment' => array(
'property_keys' => array(
'default' => 'background-attachment',
),
'path' => array( 'background', 'backgroundAttachment' ),
),
'backgroundPosition' => array(
'property_keys' => array(
'default' => 'background-position',
),
'path' => array( 'background', 'backgroundPosition' ),
),
'backgroundRepeat' => array(
'backgroundRepeat' => array(
'property_keys' => array(
'default' => 'background-repeat',
),
'path' => array( 'background', 'backgroundRepeat' ),
),
'backgroundSize' => array(
'backgroundSize' => array(
'property_keys' => array(
'default' => 'background-size',
),
Expand Down
13 changes: 13 additions & 0 deletions packages/style-engine/src/styles/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ const backgroundRepeat = {
},
};

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

const backgroundSize = {
name: 'backgroundSize',
generate: ( style: Style, options: StyleOptions ) => {
Expand All @@ -76,6 +88,7 @@ const backgroundSize = {

export default [
backgroundImage,
backgroundAttachment,
backgroundPosition,
backgroundRepeat,
backgroundSize,
Expand Down

0 comments on commit 2bdb93b

Please sign in to comment.