UNPKG

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