import type { ComponentInterface, EventEmitter } from '@stencil/core'; import { Component, Element, Event, Host, Prop, State, Watch, h } from '@stencil/core'; // TODO(FW-2845) - Use @utils/forms and @utils/logging when https://github.com/ionic-team/stencil/issues/3826 is resolved import { checkmarkOutline, removeOutline, ellipseOutline } from 'ionicons/icons'; import { config } from '../../global/config'; import { getIonMode } from '../../global/ionic-global'; import type { Color, Gesture, GestureDetail, Mode, StyleEventDetail } from '../../interface'; import type { LegacyFormController } from '../../utils/forms'; import { createLegacyFormController } from '../../utils/forms'; import { getAriaLabel, renderHiddenInput, inheritAriaAttributes } from '../../utils/helpers'; import type { Attributes } from '../../utils/helpers'; import { printIonWarning } from '../../utils/logging'; import { hapticSelection } from '../../utils/native/haptic'; import { isRTL } from '../../utils/rtl'; import { createColorClasses, hostContext } from '../../utils/theme'; import type { ToggleChangeEventDetail } from './toggle-interface'; /** * @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use. * * @slot - The label text to associate with the toggle. Use the "labelPlacement" property to control where the label is placed relative to the toggle. * * @part track - The background track of the toggle. * @part handle - The toggle handle, or knob, used to change the checked state. */ @Component({ tag: 'ion-toggle', styleUrls: { ios: 'toggle.ios.scss', md: 'toggle.md.scss', }, shadow: true, }) export class Toggle implements ComponentInterface { private inputId = `ion-tg-${toggleIds++}`; private gesture?: Gesture; private focusEl?: HTMLElement; private lastDrag = 0; private legacyFormController!: LegacyFormController; private inheritedAttributes: Attributes = {}; // This flag ensures we log the deprecation warning at most once. private hasLoggedDeprecationWarning = false; @Element() el!: HTMLIonToggleElement; @State() activated = false; /** * 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). */ @Prop({ reflect: true }) color?: Color; /** * The name of the control, which is submitted with the form data. */ @Prop() name: string = this.inputId; /** * If `true`, the toggle is selected. */ @Prop({ mutable: true }) checked = false; /** * If `true`, the user cannot interact with the toggle. */ @Prop() disabled = false; /** * The 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 ``, * it's only used when the toggle participates in a native `