/* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import { AnchorHTMLAttributes, ButtonHTMLAttributes, ElementType, HTMLAttributes, InputHTMLAttributes, RefObject } from 'react'; import { AriaBaseButtonProps, AriaButtonElementTypeProps, ButtonAria, ButtonProps, useButton } from './useButton'; import {chain} from '../utils/chain'; import {DOMAttributes} from '@react-types/shared'; import {mergeProps} from '../utils/mergeProps'; import {ToggleState} from 'react-stately/useToggleState'; export interface ToggleButtonProps extends ButtonProps { /** Whether the element should be selected (controlled). */ isSelected?: boolean; /** Whether the element should be selected (uncontrolled). */ defaultSelected?: boolean; /** Handler that is called when the element's selection state changes. */ onChange?: (isSelected: boolean) => void; } export interface AriaToggleButtonProps extends ToggleButtonProps, Omit< AriaBaseButtonProps, | 'aria-current' | 'form' | 'formAction' | 'formEncType' | 'formMethod' | 'formNoValidate' | 'formTarget' | 'name' | 'value' | 'type' >, AriaButtonElementTypeProps {} export interface AriaToggleButtonOptions extends Omit< AriaToggleButtonProps, 'children' > {} export interface ToggleButtonAria extends ButtonAria { /** Whether the button is selected. */ isSelected: boolean; /** Whether the button is disabled. */ isDisabled: boolean; } // Order with overrides is important: 'button' should be default export function useToggleButton( props: AriaToggleButtonOptions<'button'>, state: ToggleState, ref: RefObject ): ToggleButtonAria>; export function useToggleButton( props: AriaToggleButtonOptions<'a'>, state: ToggleState, ref: RefObject ): ToggleButtonAria>; export function useToggleButton( props: AriaToggleButtonOptions<'div'>, state: ToggleState, ref: RefObject ): ToggleButtonAria>; export function useToggleButton( props: AriaToggleButtonOptions<'input'>, state: ToggleState, ref: RefObject ): ToggleButtonAria>; export function useToggleButton( props: AriaToggleButtonOptions<'span'>, state: ToggleState, ref: RefObject ): ToggleButtonAria>; export function useToggleButton( props: AriaToggleButtonOptions, state: ToggleState, ref: RefObject ): ToggleButtonAria; /** * Provides the behavior and accessibility implementation for a toggle button component. * ToggleButtons allow users to toggle a selection on or off, for example switching between two * states or modes. */ export function useToggleButton( props: AriaToggleButtonOptions, state: ToggleState, ref: RefObject ): ToggleButtonAria> { const {isSelected} = state; const {isPressed, buttonProps} = useButton( { ...props, onPress: chain(state.toggle, props.onPress) }, ref ); return { isPressed, isSelected, isDisabled: props.isDisabled || false, buttonProps: mergeProps(buttonProps, { 'aria-pressed': isSelected }) }; }