UNPKG

6.3 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 /**
23 * Use continuePropagation.
24 * @deprecated */
25 stopPropagation(): void,
26 continuePropagation(): void
27}
28
29export type KeyboardEvent = BaseEvent<ReactKeyboardEvent<any>>;
30
31export type PointerType = 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';
32
33export interface PressEvent {
34 /** The type of press event being fired. */
35 type: 'pressstart' | 'pressend' | 'pressup' | 'press',
36 /** The pointer type that triggered the press event. */
37 pointerType: PointerType,
38 /** The target element of the press event. */
39 target: Element,
40 /** Whether the shift keyboard modifier was held during the press event. */
41 shiftKey: boolean,
42 /** Whether the ctrl keyboard modifier was held during the press event. */
43 ctrlKey: boolean,
44 /** Whether the meta keyboard modifier was held during the press event. */
45 metaKey: boolean,
46 /** Whether the alt keyboard modifier was held during the press event. */
47 altKey: boolean,
48 /**
49 * By default, press events stop propagation to parent elements.
50 * In cases where a handler decides not to handle a specific event,
51 * it can call `continuePropagation()` to allow a parent to handle it.
52 */
53 continuePropagation(): void
54}
55
56export interface LongPressEvent extends Omit<PressEvent, 'type' | 'continuePropagation'> {
57 /** The type of long press event being fired. */
58 type: 'longpressstart' | 'longpressend' | 'longpress'
59}
60
61export interface HoverEvent {
62 /** The type of hover event being fired. */
63 type: 'hoverstart' | 'hoverend',
64 /** The pointer type that triggered the hover event. */
65 pointerType: 'mouse' | 'pen',
66 /** The target element of the hover event. */
67 target: HTMLElement
68}
69
70export interface KeyboardEvents {
71 /** Handler that is called when a key is pressed. */
72 onKeyDown?: (e: KeyboardEvent) => void,
73 /** Handler that is called when a key is released. */
74 onKeyUp?: (e: KeyboardEvent) => void
75}
76
77export interface FocusEvents<Target = Element> {
78 /** Handler that is called when the element receives focus. */
79 onFocus?: (e: FocusEvent<Target>) => void,
80 /** Handler that is called when the element loses focus. */
81 onBlur?: (e: FocusEvent<Target>) => void,
82 /** Handler that is called when the element's focus status changes. */
83 onFocusChange?: (isFocused: boolean) => void
84}
85
86export interface HoverEvents {
87 /** Handler that is called when a hover interaction starts. */
88 onHoverStart?: (e: HoverEvent) => void,
89 /** Handler that is called when a hover interaction ends. */
90 onHoverEnd?: (e: HoverEvent) => void,
91 /** Handler that is called when the hover state changes. */
92 onHoverChange?: (isHovering: boolean) => void
93}
94
95export interface PressEvents {
96 /** Handler that is called when the press is released over the target. */
97 onPress?: (e: PressEvent) => void,
98 /** Handler that is called when a press interaction starts. */
99 onPressStart?: (e: PressEvent) => void,
100 /**
101 * Handler that is called when a press interaction ends, either
102 * over the target or when the pointer leaves the target.
103 */
104 onPressEnd?: (e: PressEvent) => void,
105 /** Handler that is called when the press state changes. */
106 onPressChange?: (isPressed: boolean) => void,
107 /**
108 * Handler that is called when a press is released over the target, regardless of
109 * whether it started on the target or not.
110 */
111 onPressUp?: (e: PressEvent) => void
112}
113
114export interface FocusableProps<Target = Element> extends FocusEvents<Target>, KeyboardEvents {
115 /** Whether the element should receive focus on render. */
116 autoFocus?: boolean
117}
118
119interface BaseMoveEvent {
120 /** The pointer type that triggered the move event. */
121 pointerType: PointerType,
122 /** Whether the shift keyboard modifier was held during the move event. */
123 shiftKey: boolean,
124 /** Whether the ctrl keyboard modifier was held during the move event. */
125 ctrlKey: boolean,
126 /** Whether the meta keyboard modifier was held during the move event. */
127 metaKey: boolean,
128 /** Whether the alt keyboard modifier was held during the move event. */
129 altKey: boolean
130}
131
132export interface MoveStartEvent extends BaseMoveEvent {
133 /** The type of move event being fired. */
134 type: 'movestart'
135}
136
137export interface MoveMoveEvent extends BaseMoveEvent {
138 /** The type of move event being fired. */
139 type: 'move',
140 /** The amount moved in the X direction since the last event. */
141 deltaX: number,
142 /** The amount moved in the Y direction since the last event. */
143 deltaY: number
144
145}
146
147export interface MoveEndEvent extends BaseMoveEvent {
148 /** The type of move event being fired. */
149 type: 'moveend'
150}
151
152export type MoveEvent = MoveStartEvent | MoveMoveEvent | MoveEndEvent;
153
154export interface MoveEvents {
155 /** Handler that is called when a move interaction starts. */
156 onMoveStart?: (e: MoveStartEvent) => void,
157 /** Handler that is called when the element is moved. */
158 onMove?: (e: MoveMoveEvent) => void,
159 /** Handler that is called when a move interaction ends. */
160 onMoveEnd?: (e: MoveEndEvent) => void
161}
162
163export interface ScrollEvent {
164 /** The amount moved in the X direction since the last event. */
165 deltaX: number,
166 /** The amount moved in the Y direction since the last event. */
167 deltaY: number
168}
169
170export interface ScrollEvents {
171 /** Handler that is called when the scroll wheel moves. */
172 onScroll?: (e: ScrollEvent) => void
173}