メインコンテンツまでスキップ
バージョン: v9

ion-toggle

shadow

トグルは、1つのオプションの状態を変更するスイッチです。押したり、スワイプしたりすることで、オン・オフを切り替えることができます。トグルは、checkedプロパティを設定することで、プログラムによってチェックすることも可能です。

基本的な使い方​

On / Off Labels​

トグルは、enableOnOffLabelsプロパティを設定することで、オン/オフラベルを有効にすることができます。これはアクセシビリティ上重要なことで、チェックされたトグルとチェックされていないトグルの区別がつきやすくなります。

Toggles in a List​

アイテムとリストコンポーネントを使用すると、リストビューでトグルを使用することも可能です。

Label Placement​

開発者は labelPlacement プロパティを使用して、ラベルがどのように配置されるかを制御することができます。

Alignment​

開発者はalignmentプロパティを使用して、ラベルとコントロールがクロス軸上でどのように配置されるかを制御できます。このプロパティは、flexboxのalign-itemsプロパティを反映しています。

注記

スタックされたトグルは、alignmentプロパティを使用して配置できます。ラベルとコントロールを水平方向に中央揃えする必要がある場合に便利です。

Justification​

開発者は justify プロパティを使用して、ラベルとコントロールの行の詰め方を制御することができます。

Helper & Error Text​

ヘルパーテキストとエラーテキストは、helperTextとerrorTextプロパティを使用してトグル内で使用できます。エラーテキストは、ion-invalidとion-touchedクラスがion-toggleに追加されていない限り表示されません。これにより、ユーザーがデータを入力する前にエラーが表示されることはありません。

Angularでは、これはフォームバリデーションによって自動的に行われます。JavaScript、React、Vueでは、独自のバリデーションに基づいてクラスを手動で追加する必要があります。

Theming​

Colors​

CSSカスタムプロパティ​

CSSカスタムプロパティは、標準CSSと組み合わせてトグルの異なる部分をターゲットにすることができます。トグルの width と height を直接変更してトラックのサイズを変更し、--handle-width と --handle-height カスタムプロパティを使用して、ハンドルサイズをカスタマイズすることができます。

CSS Shadow Parts​

トグルをさらにカスタマイズするには、公開されている特定のシャドウ部分をターゲットにすることができます。これらの部分には、どのようなCSSプロパティでもスタイルを設定でき、CSSカスタムプロパティと組み合わせることも可能です。

Interfaces​

ToggleChangeEventDetail​

interface ToggleChangeEventDetail<T = any> {
value: T;
checked: boolean;
}

ToggleCustomEvent​

必須ではありませんが、このコンポーネントから発行される Ionic イベントでより強く型付けを行うために、CustomEvent インターフェースの代わりにこのインターフェースを使用することが可能です。

interface ToggleCustomEvent<T = any> extends CustomEvent {
detail: ToggleChangeEventDetail<T>;
target: HTMLIonToggleElement;
}

プロパティ​

alignment​

DescriptionHow to control the alignment of the toggle and label on the cross axis. "start": The label and control will appear on the left of the cross axis in LTR, and on the right side in RTL. "center": The label and control will appear at the center of the cross axis in both LTR and RTL. Setting this property will change the toggle display to block.
Attributealignment
Type"center" | "start" | undefined
Defaultundefined

checked​

DescriptionIf true, the toggle is selected.
Attributechecked
Typeboolean
Defaultfalse

color​

DescriptionThe 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.
Attributecolor
Type"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
Defaultundefined

disabled​

DescriptionIf true, the user cannot interact with the toggle.
Attributedisabled
Typeboolean
Defaultfalse

enableOnOffLabels​

DescriptionEnables the on/off accessibility switch labels within the toggle.
Attributeenable-on-off-labels
Typeboolean | undefined
Defaultconfig.get('toggleOnOffLabels')

errorText​

DescriptionText that is placed under the toggle label and displayed when an error is detected.
Attributeerror-text
Typestring | undefined
Defaultundefined

helperText​

DescriptionText that is placed under the toggle label and displayed when no error is detected.
Attributehelper-text
Typestring | undefined
Defaultundefined

justify​

DescriptionHow to pack the label and toggle within a line. "start": The label and toggle will appear on the left in LTR and on the right in RTL. "end": The label and toggle will appear on the right in LTR and on the left in RTL. "space-between": The label and toggle will appear on opposite ends of the line with space between the two elements. Setting this property will change the toggle display to block.
Attributejustify
Type"end" | "space-between" | "start" | undefined
Defaultundefined

labelPlacement​

DescriptionWhere to place the label relative to the input. "start": The label will appear to the left of the toggle in LTR and to the right in RTL. "end": The label will appear to the right of the toggle in LTR and to the left in RTL. "fixed": The label has the same behavior as "start" except it also has a fixed width. Long text will be truncated with ellipses ("..."). "stacked": The label will appear above the toggle regardless of the direction. The alignment of the label can be controlled with the alignment property.
Attributelabel-placement
Type"end" | "fixed" | "stacked" | "start"
Default'start'

mode​

DescriptionThe mode determines which platform styles to use.

This is a virtual property that is set once during initialization and will not update if you change its value after the initial render.
Attributemode
Type"ios" | "md"
Defaultundefined

name​

DescriptionThe name of the control, which is submitted with the form data.
Attributename
Typestring
Defaultthis.inputId

required​

DescriptionIf true, screen readers will announce it as a required field. This property works only for accessibility purposes, it will not prevent the form from submitting if the value is invalid.
Attributerequired
Typeboolean
Defaultfalse

value​

DescriptionThe value of the toggle does not mean if it's checked or not, use the checked property for that.

The value of a toggle is analogous to the value of a <input type="checkbox">, it's only used when the toggle participates in a native <form>.
Attributevalue
Typenull | string | undefined
Default'on'

イベント​

NameDescriptionBubbles
ionBlurEmitted when the toggle loses focus.true
ionChangeEmitted when the user switches the toggle on or off.

This event will not emit when programmatically setting the checked property.
true
ionFocusEmitted when the toggle has focus.true

メソッド​

No public methods available for this component.

CSS Shadow Parts​

NameDescription
error-textSupporting text displayed beneath the toggle label when the toggle is invalid and touched.
handleThe toggle handle, or knob, used to change the checked state.
helper-textSupporting text displayed beneath the toggle label when the toggle is valid.
labelThe label text describing the toggle.
supporting-textSupporting text displayed beneath the toggle label.
trackThe background track of the toggle.

CSSカスタムプロパティ​

NameDescription
--border-radiusBorder radius of the toggle track
--handle-backgroundBackground of the toggle handle
--handle-background-checkedBackground of the toggle handle when checked
--handle-border-radiusBorder radius of the toggle handle
--handle-box-shadowBox shadow of the toggle handle
--handle-heightHeight of the toggle handle
--handle-max-heightMaximum height of the toggle handle
--handle-spacingHorizontal spacing around the toggle handle
--handle-transitionTransition of the toggle handle
--handle-widthWidth of the toggle handle
--track-backgroundBackground of the toggle track
--track-background-checkedBackground of the toggle track when checked

Slots​

NameDescription
The label text to associate with the toggle. Use the "labelPlacement" property to control where the label is placed relative to the toggle.