@zag-js/toggle
Version:
Core logic for the toggle widget implemented as a state machine
59 lines (56 loc) • 1.49 kB
text/typescript
import { Machine, EventObject, Service } from '@zag-js/core';
import { PropTypes } from '@zag-js/types';
interface ToggleProps {
/**
* Whether the toggle is disabled.
*/
disabled?: boolean | undefined;
/**
* The default pressed state of the toggle.
*/
defaultPressed?: boolean | undefined;
/**
* The pressed state of the toggle.
*/
pressed?: boolean | undefined;
/**
* Event handler called when the pressed state of the toggle changes.
*/
onPressedChange?: ((pressed: boolean) => void) | undefined;
}
interface ToggleSchema {
state: "idle";
props: ToggleProps;
context: {
pressed: boolean;
};
event: EventObject;
action: string;
guard: string;
effect: string;
}
type ToggleService = Service<ToggleSchema>;
type ToggleMachine = Machine<ToggleSchema>;
interface ToggleApi<T extends PropTypes = PropTypes> {
/**
* Whether the toggle is pressed.
*/
pressed: boolean;
/**
* Whether the toggle is disabled.
*/
disabled: boolean;
/**
* Sets the pressed state of the toggle.
*/
setPressed: (pressed: boolean) => void;
/**
* Props for the root `button` element.
*/
getRootProps: () => T["element"];
/**
* Props for the optional visual indicator (e.g. icon) element.
*/
getIndicatorProps: () => T["element"];
}
export type { ToggleApi, ToggleMachine, ToggleProps, ToggleSchema, ToggleService };