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

InputControl: added password visibility story #60898

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

- `InputControl`: Add a password visibility toggle story ([#60898](https://github.com/WordPress/gutenberg/pull/60898)).
- `BoxControl`: Allow negative values for margin controls ([#60347](https://github.com/WordPress/gutenberg/pull/60347)).

### Bug Fix
Expand Down
32 changes: 31 additions & 1 deletion packages/components/src/input-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';

/**
* WordPress dependencies
*/
import { seen, unseen } from '@wordpress/icons';
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import InputControl from '..';
import { InputControlPrefixWrapper } from '../input-prefix-wrapper';
import { InputControlSuffixWrapper } from '../input-suffix-wrapper';
import Button from '../../button';

const meta: Meta< typeof InputControl > = {
title: 'Components (Experimental)/InputControl',
Expand Down Expand Up @@ -82,3 +87,28 @@ WithEdgeLabel.args = {
__unstableInputWidth: '20em',
labelPosition: 'edge',
};

export const ShowPassword: StoryFn< typeof InputControl > = ( args ) => {
const [ visible, setVisible ] = useState( false );
return (
<InputControl
type={ visible ? 'text' : 'password' }
label="Password"
suffix={
<InputControlSuffixWrapper>
<div style={ { display: 'flex' } }>
<Button
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use the size="small" variant so the focus rings don't overlap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

Copy link
Contributor Author

@DaniGuardiola DaniGuardiola Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what's the right way to style things in this context, so I went with inline styles to vertically center it. I also originally went with flex + align items center, but then realized flex was enough (not sure why though). Any suggestions welcome!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's just the fact the Button became display: block because of the flex parent. Which is equivalent to setting line-height: 0.

An inline style is perfectly appropriate here 👍

size="small"
icon={ visible ? unseen : seen }
onClick={ () => setVisible( ( value ) => ! value ) }
label={
visible ? 'Hide password' : 'Show password'
}
/>
</div>
</InputControlSuffixWrapper>
}
{ ...args }
/>
);
};
Loading