Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storybook: Add stories for the TextAlignmentControl component #67371

Merged
Prev Previous commit
Next Next commit
Storybook: Update TextAlignmentControl story to follow best practices…
… and simplify the structure
  • Loading branch information
himanshupathak95 committed Dec 10, 2024
commit 8c8026f3dadcb2fb74357d2bcb9b00a98ce8ec9b
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -13,93 +13,60 @@ import TextAlignmentControl from '../';
*/
const ALIGNMENT_OPTIONS = [ 'left', 'center', 'right', 'justify' ];

/**
* Demo text component to show text alignment effect
*/
const DemoText = ( { alignment } ) => (
<p style={ { textAlign: alignment } }>
This is a sample paragraph to demonstrate the text alignment control. It
contains multiple lines of text to better show the alignment effect. You
can see how the text aligns differently based on the selected option.
</p>
);

/**
* TextAlignmentControl Properties
*/
export default {
const meta = {
title: 'BlockEditor/TextAlignmentControl',
component: TextAlignmentControl,
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
parameters: {
docs: {
canvas: { sourceState: 'shown' },
description: {
component: 'Control to facilitate text alignment selections.',
},
},
},
argTypes: {
value: {
control: 'select',
options: ALIGNMENT_OPTIONS,
description: 'Currently selected text alignment value',
control: { type: null },
description: 'Currently selected text alignment value.',
table: {
type: {
summary: 'string',
},
defaultValue: { summary: 'left' },
},
},
onChange: {
action: 'onChange',
control: {
type: null,
},
description: 'Callback function when text alignment changes',
control: { type: null },
description: 'Handles change in text alignment selection.',
table: {
type: {
summary: 'function',
},
},
},
options: {
control: 'array',
description: 'Array of alignment options to display',
control: 'check',
description: 'Array of text alignment options to display.',
options: ALIGNMENT_OPTIONS,
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
table: {
type: { summary: 'array' },
defaultValue: {
summary: "['left', 'center', 'right', 'justify']",
},
},
},
className: {
control: 'text',
description: 'Additional CSS class name to apply',
description: 'Class name to add to the control.',
table: {
type: { summary: 'string' },
},
},
},
render: function Render( { onChange, value, options, className } ) {
const [ selectedAlignment, setSelectedAlignment ] = useState( value );

useEffect( () => {
setSelectedAlignment( value );
}, [ value ] );

const handleAlignmentChange = ( newValue ) => {
setSelectedAlignment( newValue );
if ( onChange ) {
onChange( newValue );
}
};

return (
<div>
<TextAlignmentControl
value={ selectedAlignment }
onChange={ handleAlignmentChange }
options={ options }
className={ className }
/>
<DemoText alignment={ selectedAlignment } />
</div>
);
},
};

export default meta;

/**
* Default story showing TextAlignmentControl with left alignment
*/
Expand All @@ -108,55 +75,17 @@ export const Default = {
value: 'left',
options: ALIGNMENT_OPTIONS,
},
};

/**
* TextAlignmentControl with left alignment
*/
export const WithLeftAlignment = {
args: {
value: 'left',
options: ALIGNMENT_OPTIONS,
},
};

/**
* TextAlignmentControl with center alignment
*/
export const WithCenterAlignment = {
args: {
value: 'center',
options: ALIGNMENT_OPTIONS,
},
};

/**
* TextAlignmentControl with right alignment
*/
export const WithRightAlignment = {
args: {
value: 'right',
options: ALIGNMENT_OPTIONS,
},
};

/**
* TextAlignmentControl with justify alignment
*/
export const WithJustifyAlignment = {
args: {
value: 'justify',
options: ALIGNMENT_OPTIONS,
},
};

/**
* TextAlignmentControl with custom className
*/
export const WithCustomClass = {
args: {
value: 'left',
options: ALIGNMENT_OPTIONS,
className: 'custom-text-alignment-control',
render: function Template( { onChange, ...args } ) {
const [ value, setValue ] = useState();
return (
<TextAlignmentControl
{ ...args }
onChange={ ( ...changeArgs ) => {
onChange( ...changeArgs );
setValue( ...changeArgs );
} }
value={ value }
/>
);
},
};