UNPKG

5.95 kBTypeScriptView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13import {
14 FocusEvent,
15 KeyboardEvent as ReactKeyboardEvent,
16 SyntheticEvent
17} from 'react';
18
19// Event bubbling can be problematic in real-world applications, so the default for React Spectrum components
20// is not to propagate. This can be overridden by calling continuePropagation() on the event.
21export type BaseEvent<T extends SyntheticEvent> = T & {
22 /** @deprecated Use continuePropagation. */
23 stopPropagation(): void,
24 continuePropagation(): void
25}
26
27export type KeyboardEvent = BaseEvent<ReactKeyboardEvent<any>>;
28
29export type PointerType = 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';
30
31export interface PressEvent {
32 /** The type of press event being fired. */
33 type: 'pressstart' | 'pressend' | 'pressup' | 'press',
34 /** The pointer type that triggered the press event. */
35 pointerType: PointerType,
36 /** The target element of the press event. */
37 target: HTMLElement,
38 /** Whether the shift keyboard modifier was held during the press event. */
39 shiftKey: boolean,
40 /** Whether the ctrl keyboard modifier was held during the press event. */
41 ctrlKey: boolean,
42 /** Whether the meta keyboard modifier was held during the press event. */
43 metaKey: boolean,
44 /** Whether the alt keyboard modifier was held during the press event. */
45 altKey: boolean
46}
47
48export interface LongPressEvent extends Omit<PressEvent, 'type'> {
49 /** The type of long press event being fired. */
50 type: 'longpressstart' | 'longpressend' | 'longpress'
51}
52
53export interface HoverEvent {
54 /** The type of hover event being fired. */
55 type: 'hoverstart' | 'hoverend',
56 /** The pointer type that triggered the hover event. */
57 pointerType: 'mouse' | 'pen',
58 /** The target element of the hover event. */
59 target: HTMLElement
60}
61
62export interface KeyboardEvents {
63 /** Handler that is called when a key is pressed. */
64 onKeyDown?: (e: KeyboardEvent) => void,
65 /** Handler that is called when a key is released. */
66 onKeyUp?: (e: KeyboardEvent) => void
67}
68
69export interface FocusEvents {
70 /** Handler that is called when the element receives focus. */
71 onFocus?: (e: FocusEvent) => void,
72 /** Handler that is called when the element loses focus. */
73 onBlur?: (e: FocusEvent) => void,
74 /** Handler that is called when the element's focus status changes. */
75 onFocusChange?: (isFocused: boolean) => void
76}
77
78export interface HoverEvents {
79 /** Handler that is called when a hover interaction starts. */
80 onHoverStart?: (e: HoverEvent) => void,
81 /** Handler that is called when a hover interaction ends. */
82 onHoverEnd?: (e: HoverEvent) => void,
83 /** Handler that is called when the hover state changes. */
84 onHoverChange?: (isHovering: boolean) => void
85}
86
87export interface PressEvents {
88 /** Handler that is called when the press is released over the target. */
89 onPress?: (e: PressEvent) => void,
90 /** Handler that is called when a press interaction starts. */
91 onPressStart?: (e: PressEvent) => void,
92 /**
93 * Handler that is called when a press interaction ends, either
94 * over the target or when the pointer leaves the target.
95 */
96 onPressEnd?: (e: PressEvent) => void,
97 /** Handler that is called when the press state changes. */
98 onPressChange?: (isPressed: boolean) => void,
99 /**
100 * Handler that is called when a press is released over the target, regardless of
101 * whether it started on the target or not.
102 */
103 onPressUp?: (e: PressEvent) => void
104}
105
106export interface FocusableProps extends FocusEvents, KeyboardEvents {
107 /** Whether the element should receive focus on render. */
108 autoFocus?: boolean
109}
110
111interface BaseMoveEvent {
112 /** The pointer type that triggered the move event. */
113 pointerType: PointerType,
114 /** Whether the shift keyboard modifier was held during the move event. */
115 shiftKey: boolean,
116 /** Whether the ctrl keyboard modifier was held during the move event. */
117 ctrlKey: boolean,
118 /** Whether the meta keyboard modifier was held during the move event. */
119 metaKey: boolean,
120 /** Whether the alt keyboard modifier was held during the move event. */
121 altKey: boolean
122}
123
124export interface MoveStartEvent extends BaseMoveEvent {
125 /** The type of move event being fired. */
126 type: 'movestart'
127}
128
129export interface MoveMoveEvent extends BaseMoveEvent {
130 /** The type of move event being fired. */
131 type: 'move',
132 /** The amount moved in the X direction since the last event. */
133 deltaX: number,
134 /** The amount moved in the Y direction since the last event. */
135 deltaY: number
136
137}
138
139export interface MoveEndEvent extends BaseMoveEvent {
140 /** The type of move event being fired. */
141 type: 'moveend'
142}
143
144export type MoveEvent = MoveStartEvent | MoveMoveEvent | MoveEndEvent;
145
146export interface MoveEvents {
147 /** Handler that is called when a move interaction starts. */
148 onMoveStart?: (e: MoveStartEvent) => void,
149 /** Handler that is called when the element is moved. */
150 onMove?: (e: MoveMoveEvent) => void,
151 /** Handler that is called when a move interaction ends. */
152 onMoveEnd?: (e: MoveEndEvent) => void
153}
154
155export interface ScrollEvent {
156 /** The amount moved in the X direction since the last event. */
157 deltaX: number,
158 /** The amount moved in the Y direction since the last event. */
159 deltaY: number
160}
161
162export interface ScrollEvents {
163 /** Handler that is called when the scroll wheel moves. */
164 onScroll?: (e: ScrollEvent) => void
165}