Skip to content

Commit

Permalink
Add tooltip to variation
Browse files Browse the repository at this point in the history
  • Loading branch information
amitraj2203 committed Jun 21, 2024
1 parent 0dcbf97 commit 55f3afa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { Tooltip } from '@wordpress/components';
import { useMemo, useContext, useState } from '@wordpress/element';
import { ENTER } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
Expand All @@ -23,7 +24,13 @@ const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock(
blockEditorPrivateApis
);

export default function Variation( { variation, children, isPill, property } ) {
export default function Variation( {
variation,
children,
isPill,
property,
showTooltip,
} ) {
const [ isFocused, setIsFocused ] = useState( false );
const { base, user, setUserConfig } = useContext( GlobalStylesContext );

Expand Down Expand Up @@ -64,30 +71,38 @@ export default function Variation( { variation, children, isPill, property } ) {
);
}

return (
<GlobalStylesContext.Provider value={ context }>
const content = (
<div
className={ clsx( 'edit-site-global-styles-variations_item', {
'is-active': isActive,
} ) }
role="button"
onClick={ selectVariation }
onKeyDown={ selectOnEnter }
tabIndex="0"
aria-label={ label }
aria-current={ isActive }
onFocus={ () => setIsFocused( true ) }
onBlur={ () => setIsFocused( false ) }
>
<div
className={ clsx( 'edit-site-global-styles-variations_item', {
'is-active': isActive,
} ) }
role="button"
onClick={ selectVariation }
onKeyDown={ selectOnEnter }
tabIndex="0"
aria-label={ label }
aria-current={ isActive }
onFocus={ () => setIsFocused( true ) }
onBlur={ () => setIsFocused( false ) }
className={ clsx(
'edit-site-global-styles-variations_item-preview',
{ 'is-pill': isPill }
) }
>
<div
className={ clsx(
'edit-site-global-styles-variations_item-preview',
{ 'is-pill': isPill }
) }
>
{ children( isFocused ) }
</div>
{ children( isFocused ) }
</div>
</div>
);

return (
<GlobalStylesContext.Provider value={ context }>
{ showTooltip ? (
<Tooltip text={ variation?.title }>{ content }</Tooltip>
) : (
content
) }
</GlobalStylesContext.Provider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
__experimentalVStack as VStack,
__experimentalGrid as Grid,
Tooltip,
} from '@wordpress/components';

/**
Expand All @@ -28,18 +27,15 @@ export default function ColorVariations( { title, gap = 2 } ) {
{ title && <Subtitle level={ 3 }>{ title }</Subtitle> }
<Grid spacing={ gap }>
{ colorVariations.map( ( variation, index ) => (
<Tooltip key={ index } text={ variation?.title }>
{ /* This div is needed for Tooltips to work */ }
<div>
<Variation
variation={ variation }
isPill
property="color"
>
{ () => <StylesPreviewColors /> }
</Variation>
</div>
</Tooltip>
<Variation
key={ index }
variation={ variation }
isPill
property="color"
showTooltip
>
{ () => <StylesPreviewColors /> }
</Variation>
) ) }
</Grid>
</VStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default function TypographyVariations( { title, gap = 2 } ) {
key={ index }
variation={ variation }
property="typography"
showTooltip
>
{ ( isFocused ) => (
<PreviewIframe
Expand Down

0 comments on commit 55f3afa

Please sign in to comment.