Skip to content

Commit 6c500fd

Browse files
feat(input): add input-password-toggle component (#29175)
Issue number: Internal --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> When given a password input it is hard to know what users are typing as the contents of the input are obscured. As a result, it is a common pattern to have a button that lets users temporarily toggle the visibility of the password so they can correct any mistakes. Ionic currently has the infrastructure for developers to implement this on their own, but this use case is so common that the team thinks it is worth having this functionality built-in to Ionic. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Introduces the `ion-input-password-toggle` component. This component is a button that toggles the visibility of the text in the input it is slotted into. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> ⚠️ Give co-author credit to #29141 on merge. Docs PR: ionic-team/ionic-docs#3541 Note: We did not do the approach listed in the other PR due to #29141 (comment). --------- Co-authored-by: OS-giulianasilva <[email protected]>
1 parent 6e477b7 commit 6c500fd

19 files changed

+563
-4
lines changed

core/api.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ ion-input,prop,color,"danger" | "dark" | "light" | "medium" | "primary" | "secon
558558
ion-input,prop,counter,boolean,false,false,false
559559
ion-input,prop,counterFormatter,((inputLength: number, maxLength: number) => string) | undefined,undefined,false,false
560560
ion-input,prop,debounce,number | undefined,undefined,false,false
561-
ion-input,prop,disabled,boolean,false,false,false
561+
ion-input,prop,disabled,boolean,false,false,true
562562
ion-input,prop,enterkeyhint,"done" | "enter" | "go" | "next" | "previous" | "search" | "send" | undefined,undefined,false,false
563563
ion-input,prop,errorText,string | undefined,undefined,false,false
564564
ion-input,prop,fill,"outline" | "solid" | undefined,undefined,false,false
@@ -575,7 +575,7 @@ ion-input,prop,multiple,boolean | undefined,undefined,false,false
575575
ion-input,prop,name,string,this.inputId,false,false
576576
ion-input,prop,pattern,string | undefined,undefined,false,false
577577
ion-input,prop,placeholder,string | undefined,undefined,false,false
578-
ion-input,prop,readonly,boolean,false,false,false
578+
ion-input,prop,readonly,boolean,false,false,true
579579
ion-input,prop,required,boolean,false,false,false
580580
ion-input,prop,shape,"round" | undefined,undefined,false,false
581581
ion-input,prop,spellcheck,boolean,false,false,false
@@ -607,6 +607,12 @@ ion-input,css-prop,--placeholder-font-style
607607
ion-input,css-prop,--placeholder-font-weight
608608
ion-input,css-prop,--placeholder-opacity
609609

610+
ion-input-password-toggle,shadow
611+
ion-input-password-toggle,prop,color,"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string & Record<never, never> | undefined,undefined,false,true
612+
ion-input-password-toggle,prop,hideIcon,string | undefined,undefined,false,false
613+
ion-input-password-toggle,prop,mode,"ios" | "md",undefined,false,false
614+
ion-input-password-toggle,prop,showIcon,string | undefined,undefined,false,false
615+
610616
ion-item,shadow
611617
ion-item,prop,button,boolean,false,false,false
612618
ion-item,prop,color,"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string & Record<never, never> | undefined,undefined,false,true

core/src/components.d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,25 @@ export namespace Components {
12871287
*/
12881288
"value"?: string | number | null;
12891289
}
1290+
interface IonInputPasswordToggle {
1291+
/**
1292+
* The color to use from your application's color palette. Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`. For more information on colors, see [theming](/docs/theming/basics).
1293+
*/
1294+
"color"?: Color;
1295+
/**
1296+
* The icon that can be used to represent hiding a password. If not set, the "eyeOff" Ionicon will be used.
1297+
*/
1298+
"hideIcon"?: string;
1299+
/**
1300+
* The mode determines which platform styles to use.
1301+
*/
1302+
"mode"?: "ios" | "md";
1303+
/**
1304+
* The icon that can be used to represent showing a password. If not set, the "eye" Ionicon will be used.
1305+
*/
1306+
"showIcon"?: string;
1307+
"type": TextFieldTypes;
1308+
}
12901309
interface IonItem {
12911310
/**
12921311
* If `true`, a button tag will be rendered and the item will be tappable.
@@ -3811,6 +3830,12 @@ declare global {
38113830
prototype: HTMLIonInputElement;
38123831
new (): HTMLIonInputElement;
38133832
};
3833+
interface HTMLIonInputPasswordToggleElement extends Components.IonInputPasswordToggle, HTMLStencilElement {
3834+
}
3835+
var HTMLIonInputPasswordToggleElement: {
3836+
prototype: HTMLIonInputPasswordToggleElement;
3837+
new (): HTMLIonInputPasswordToggleElement;
3838+
};
38143839
interface HTMLIonItemElement extends Components.IonItem, HTMLStencilElement {
38153840
}
38163841
var HTMLIonItemElement: {
@@ -4635,6 +4660,7 @@ declare global {
46354660
"ion-infinite-scroll": HTMLIonInfiniteScrollElement;
46364661
"ion-infinite-scroll-content": HTMLIonInfiniteScrollContentElement;
46374662
"ion-input": HTMLIonInputElement;
4663+
"ion-input-password-toggle": HTMLIonInputPasswordToggleElement;
46384664
"ion-item": HTMLIonItemElement;
46394665
"ion-item-divider": HTMLIonItemDividerElement;
46404666
"ion-item-group": HTMLIonItemGroupElement;
@@ -5993,6 +6019,25 @@ declare namespace LocalJSX {
59936019
*/
59946020
"value"?: string | number | null;
59956021
}
6022+
interface IonInputPasswordToggle {
6023+
/**
6024+
* The color to use from your application's color palette. Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`. For more information on colors, see [theming](/docs/theming/basics).
6025+
*/
6026+
"color"?: Color;
6027+
/**
6028+
* The icon that can be used to represent hiding a password. If not set, the "eyeOff" Ionicon will be used.
6029+
*/
6030+
"hideIcon"?: string;
6031+
/**
6032+
* The mode determines which platform styles to use.
6033+
*/
6034+
"mode"?: "ios" | "md";
6035+
/**
6036+
* The icon that can be used to represent showing a password. If not set, the "eye" Ionicon will be used.
6037+
*/
6038+
"showIcon"?: string;
6039+
"type"?: TextFieldTypes;
6040+
}
59966041
interface IonItem {
59976042
/**
59986043
* If `true`, a button tag will be rendered and the item will be tappable.
@@ -8040,6 +8085,7 @@ declare namespace LocalJSX {
80408085
"ion-infinite-scroll": IonInfiniteScroll;
80418086
"ion-infinite-scroll-content": IonInfiniteScrollContent;
80428087
"ion-input": IonInput;
8088+
"ion-input-password-toggle": IonInputPasswordToggle;
80438089
"ion-item": IonItem;
80448090
"ion-item-divider": IonItemDivider;
80458091
"ion-item-group": IonItemGroup;
@@ -8138,6 +8184,7 @@ declare module "@stencil/core" {
81388184
"ion-infinite-scroll": LocalJSX.IonInfiniteScroll & JSXBase.HTMLAttributes<HTMLIonInfiniteScrollElement>;
81398185
"ion-infinite-scroll-content": LocalJSX.IonInfiniteScrollContent & JSXBase.HTMLAttributes<HTMLIonInfiniteScrollContentElement>;
81408186
"ion-input": LocalJSX.IonInput & JSXBase.HTMLAttributes<HTMLIonInputElement>;
8187+
"ion-input-password-toggle": LocalJSX.IonInputPasswordToggle & JSXBase.HTMLAttributes<HTMLIonInputPasswordToggleElement>;
81418188
"ion-item": LocalJSX.IonItem & JSXBase.HTMLAttributes<HTMLIonItemElement>;
81428189
"ion-item-divider": LocalJSX.IonItemDivider & JSXBase.HTMLAttributes<HTMLIonItemDividerElement>;
81438190
"ion-item-group": LocalJSX.IonItemGroup & JSXBase.HTMLAttributes<HTMLIonItemGroupElement>;

core/src/components/input-password-toggle/input-password-toggle.scss

Whitespace-only changes.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import type { ComponentInterface } from '@stencil/core';
2+
import { Component, Element, Host, Prop, h, Watch } from '@stencil/core';
3+
import { printIonWarning } from '@utils/logging';
4+
import { createColorClasses } from '@utils/theme';
5+
import { eyeOff, eye } from 'ionicons/icons';
6+
7+
import { getIonMode } from '../../global/ionic-global';
8+
import type { Color, TextFieldTypes } from '../../interface';
9+
10+
/**
11+
* @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use.
12+
*/
13+
@Component({
14+
tag: 'ion-input-password-toggle',
15+
/**
16+
* Empty CSS files are required in order for the mode to be inherited to the
17+
* inner ion-button. Otherwise, the setMode callback provided to Stencil will not get called
18+
* and we will default to MD mode.
19+
*/
20+
styleUrls: {
21+
ios: 'input-password-toggle.scss',
22+
md: 'input-password-toggle.scss',
23+
},
24+
shadow: true,
25+
})
26+
export class InputPasswordToggle implements ComponentInterface {
27+
private inputElRef!: HTMLIonInputElement | null;
28+
29+
@Element() el!: HTMLIonInputElement;
30+
31+
/**
32+
* The color to use from your application's color palette.
33+
* Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`.
34+
* For more information on colors, see [theming](/docs/theming/basics).
35+
*/
36+
@Prop({ reflect: true }) color?: Color;
37+
38+
/**
39+
* The icon that can be used to represent showing a password. If not set, the "eye" Ionicon will be used.
40+
*/
41+
@Prop() showIcon?: string;
42+
43+
/**
44+
* The icon that can be used to represent hiding a password. If not set, the "eyeOff" Ionicon will be used.
45+
*/
46+
@Prop() hideIcon?: string;
47+
48+
/**
49+
* @internal
50+
*/
51+
@Prop({ mutable: true }) type: TextFieldTypes = 'password';
52+
53+
/**
54+
* Whenever the input type changes we need to re-run validation to ensure the password
55+
* toggle is being used with the correct input type. If the application changes the type
56+
* outside of this component we also need to re-render so the correct icon is shown.
57+
*/
58+
@Watch('type')
59+
onTypeChange(newValue: TextFieldTypes) {
60+
if (newValue !== 'text' && newValue !== 'password') {
61+
printIonWarning(
62+
`ion-input-password-toggle only supports inputs of type "text" or "password". Input of type "${newValue}" is not compatible.`,
63+
this.el
64+
);
65+
66+
return;
67+
}
68+
}
69+
70+
connectedCallback() {
71+
const { el } = this;
72+
73+
const inputElRef = (this.inputElRef = el.closest('ion-input'));
74+
75+
if (!inputElRef) {
76+
printIonWarning(
77+
'No ancestor ion-input found for ion-input-password-toggle. This component must be slotted inside of an ion-input.',
78+
el
79+
);
80+
81+
return;
82+
}
83+
84+
/**
85+
* Important: Set the type in connectedCallback because the default value
86+
* of this.type may not always be accurate. Usually inputs have the "password" type
87+
* but it is possible to have the input to initially have the "text" type. In that scenario
88+
* the wrong icon will show briefly before switching to the correct icon. Setting the
89+
* type here allows us to avoid that flicker.
90+
*/
91+
this.type = inputElRef.type;
92+
}
93+
94+
disconnectedCallback() {
95+
this.inputElRef = null;
96+
}
97+
98+
private togglePasswordVisibility = () => {
99+
const { inputElRef } = this;
100+
101+
if (!inputElRef) {
102+
return;
103+
}
104+
105+
inputElRef.type = inputElRef.type === 'text' ? 'password' : 'text';
106+
};
107+
108+
render() {
109+
const { color, type } = this;
110+
111+
const mode = getIonMode(this);
112+
113+
const showPasswordIcon = this.showIcon ?? eye;
114+
const hidePasswordIcon = this.hideIcon ?? eyeOff;
115+
116+
const isPasswordVisible = type === 'text';
117+
118+
return (
119+
<Host
120+
class={createColorClasses(color, {
121+
[mode]: true,
122+
})}
123+
>
124+
<ion-button
125+
mode={mode}
126+
color={color}
127+
fill="clear"
128+
shape="round"
129+
aria-checked={isPasswordVisible ? 'true' : 'false'}
130+
aria-label="show password"
131+
role="switch"
132+
type="button"
133+
onPointerDown={(ev) => {
134+
/**
135+
* This prevents mobile browsers from
136+
* blurring the input when the password toggle
137+
* button is activated.
138+
*/
139+
ev.preventDefault();
140+
}}
141+
onClick={this.togglePasswordVisibility}
142+
>
143+
<ion-icon
144+
slot="icon-only"
145+
aria-hidden="true"
146+
icon={isPasswordVisible ? hidePasswordIcon : showPasswordIcon}
147+
></ion-icon>
148+
</ion-button>
149+
</Host>
150+
);
151+
}
152+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import AxeBuilder from '@axe-core/playwright';
2+
import { expect } from '@playwright/test';
3+
import { configs, test } from '@utils/test/playwright';
4+
5+
configs({ directions: ['ltr'] }).forEach(({ title, config }) => {
6+
test.describe(title('input password toggle: a11y'), () => {
7+
test('should not have accessibility violations', async ({ page }) => {
8+
await page.setContent(
9+
`
10+
<main>
11+
<ion-input label="input" type="password">
12+
<ion-input-password-toggle slot="end"></ion-input-password-toggle>
13+
</ion-input>
14+
</main>
15+
`,
16+
config
17+
);
18+
19+
const results = await new AxeBuilder({ page }).analyze();
20+
expect(results.violations).toEqual([]);
21+
});
22+
});
23+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Input - Toggle Password</title>
6+
<meta
7+
name="viewport"
8+
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
9+
/>
10+
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
11+
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
12+
<script src="../../../../../scripts/testing/scripts.js"></script>
13+
<script nomodule src="../../../../../dist/ionic/ionic.js"></script>
14+
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
15+
<style>
16+
.grid {
17+
display: grid;
18+
grid-template-columns: repeat(5, minmax(250px, 1fr));
19+
grid-row-gap: 20px;
20+
grid-column-gap: 20px;
21+
}
22+
h2 {
23+
font-size: 12px;
24+
font-weight: normal;
25+
26+
color: #6f7378;
27+
28+
margin-top: 10px;
29+
}
30+
@media screen and (max-width: 800px) {
31+
.grid {
32+
grid-template-columns: 1fr;
33+
padding: 0;
34+
}
35+
}
36+
</style>
37+
</head>
38+
39+
<body>
40+
<ion-app>
41+
<ion-header>
42+
<ion-toolbar>
43+
<ion-title>Input - Basic</ion-title>
44+
</ion-toolbar>
45+
</ion-header>
46+
47+
<ion-content id="content" class="ion-padding">
48+
<div class="grid">
49+
<div class="grid-item">
50+
<h2>Default</h2>
51+
<ion-input type="password" value="supersecurepassword" label="Password">
52+
<ion-input-password-toggle slot="end"></ion-input-password-toggle>
53+
</ion-input>
54+
</div>
55+
<div class="grid-item">
56+
<h2>Custom Icon</h2>
57+
<ion-input type="password" value="supersecurepassword" label="Password">
58+
<ion-input-password-toggle show-icon="trash" slot="end"></ion-input-password-toggle>
59+
</ion-input>
60+
</div>
61+
<div class="grid-item">
62+
<h2>Custom Mode/Color</h2>
63+
<ion-input type="password" value="supersecurepassword" label="Password">
64+
<ion-input-password-toggle
65+
color="danger"
66+
mode="ios"
67+
show-icon="trash"
68+
slot="end"
69+
></ion-input-password-toggle>
70+
</ion-input>
71+
</div>
72+
</div>
73+
</ion-content>
74+
</ion-app>
75+
</body>
76+
</html>

0 commit comments

Comments
 (0)