Skip to content
Merged
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added an `__unstable-large` size variant to `InputControl`, `SelectControl`, and `UnitControl` for selective migration to the larger 40px heights. ([#35646](https://github.com/WordPress/gutenberg/pull/35646)).
- Fixed inconsistent padding in `UnitControl` ([#35646](https://github.com/WordPress/gutenberg/pull/35646)).
- Added support for RTL behavior for the `ZStack`'s `offset` prop ([#36769](https://github.com/WordPress/gutenberg/pull/36769))
- Fixed race conditions causing conditionally displayed `ToolsPanelItem` components to be erroneously deregistered ([36588](https://github.com/WordPress/gutenberg/pull/36588)).

### Bug Fix

Expand Down
183 changes: 183 additions & 0 deletions packages/components/src/tools-panel/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import styled from '@emotion/styled';
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';

/**
* Internal dependencies
Expand Down Expand Up @@ -265,6 +269,185 @@ export const WithSlotFillItems = () => {
);
};

export const WithConditionalDefaultControl = () => {
const [ attributes, setAttributes ] = useState( {} );
const { height, scale } = attributes;

const resetAll = ( resetFilters = [] ) => {
let newAttributes = {};

resetFilters.forEach( ( resetFilter ) => {
newAttributes = {
...newAttributes,
...resetFilter( newAttributes ),
};
} );

setAttributes( newAttributes );
};

const updateAttribute = ( name, value ) => {
setAttributes( {
...attributes,
[ name ]: value,
} );
};

return (
<SlotFillProvider>
<ToolsPanelItems>
<SingleColumnItem
hasValue={ () => !! height }
label="Injected Height"
onDeselect={ () => updateAttribute( 'height', undefined ) }
resetAllFilter={ () => ( { height: undefined } ) }
panelId={ panelId }
isShownByDefault={ true }
>
<UnitControl
label="Injected Height"
value={ height }
onChange={ ( next ) =>
updateAttribute( 'height', next )
}
/>
</SingleColumnItem>
<ToolsPanelItem
hasValue={ () => !! scale }
label="Scale"
onDeselect={ () => updateAttribute( 'scale', undefined ) }
resetAllFilter={ () => ( { scale: undefined } ) }
panelId={ panelId }
isShownByDefault={ !! height }
>
<ToggleGroupControl
label="Scale"
value={ scale }
onChange={ ( next ) =>
updateAttribute( 'scale', next )
}
isBlock
>
<ToggleGroupControlOption value="cover" label="Cover" />
<ToggleGroupControlOption
value="contain"
label="Contain"
/>
<ToggleGroupControlOption value="fill" label="Fill" />
</ToggleGroupControl>
</ToolsPanelItem>
</ToolsPanelItems>
<PanelWrapperView>
<Panel>
<ToolsPanel
label="Tools Panel With Conditional Default via SlotFill"
resetAll={ resetAll }
panelId={ panelId }
>
<Slot />
</ToolsPanel>
</Panel>
</PanelWrapperView>
</SlotFillProvider>
);
};

export const WithConditionallyRenderedControl = () => {
const [ attributes, setAttributes ] = useState( {} );
const { height, scale } = attributes;

const resetAll = ( resetFilters = [] ) => {
let newAttributes = {};

resetFilters.forEach( ( resetFilter ) => {
newAttributes = {
...newAttributes,
...resetFilter( newAttributes ),
};
} );

setAttributes( newAttributes );
};

const updateAttribute = ( name, value ) => {
setAttributes( {
...attributes,
[ name ]: value,
} );
};

return (
<SlotFillProvider>
<ToolsPanelItems>
<SingleColumnItem
hasValue={ () => !! height }
label="Injected Height"
onDeselect={ () => {
updateAttribute( 'scale', undefined );
updateAttribute( 'height', undefined );
} }
resetAllFilter={ () => ( { height: undefined } ) }
panelId={ panelId }
isShownByDefault={ true }
>
<UnitControl
label="Injected Height"
value={ height }
onChange={ ( next ) =>
updateAttribute( 'height', next )
}
/>
</SingleColumnItem>
{ !! height && (
<ToolsPanelItem
hasValue={ () => !! scale }
label="Scale"
onDeselect={ () =>
updateAttribute( 'scale', undefined )
}
resetAllFilter={ () => ( { scale: undefined } ) }
panelId={ panelId }
isShownByDefault={ true }
>
<ToggleGroupControl
label="Scale"
value={ scale }
onChange={ ( next ) =>
updateAttribute( 'scale', next )
}
isBlock
>
<ToggleGroupControlOption
value="cover"
label="Cover"
/>
<ToggleGroupControlOption
value="contain"
label="Contain"
/>
<ToggleGroupControlOption
value="fill"
label="Fill"
/>
</ToggleGroupControl>
</ToolsPanelItem>
) }
</ToolsPanelItems>
<PanelWrapperView>
<Panel>
<ToolsPanel
label="Tools Panel With Conditionally Rendered Item via SlotFill"
resetAll={ resetAll }
panelId={ panelId }
>
<Slot />
</ToolsPanel>
</Panel>
</PanelWrapperView>
</SlotFillProvider>
);
};

export { TypographyPanel } from './typography-panel';

const PanelWrapperView = styled.div`
Expand Down
Loading