-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
562 lines (558 loc) · 18.2 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// react
import {
HTMLAttributes,
CSSProperties,
PropsWithChildren,
MouseEventHandler,
TouchEventHandler,
FocusEventHandler,
FC,
RefObject,
} from 'react';
// csstype
import { Property } from 'csstype';
// types
export type SpotGlarePosition = 'top' | 'right' | 'bottom' | 'left' | 'all';
export type LineGlareDirection = 'to-bottom-right' | 'to-bottom-left';
export type LineGlareHoverPosition =
| 'top-left'
| 'top-right'
| 'bottom-left'
| 'bottom-right';
export type ShadowType = 'box' | 'drop';
export interface Angle {
angleX: number;
angleY: number;
}
export interface Position {
x: number;
y: number;
}
export interface Offset {
offsetX: number;
offsetY: number;
}
// ref
export interface TiltRef {
/**
* Tilts the component to the given angle
*
* @param {Angle} angle - Tilt angle ({angleX: number, angleY: number})
* @param {boolean} [changeScaleAndShadow=false] - Whether to apply the scale and shadow properties or not
* @param {boolean} [gyro=false] - Whether the event is triggered by gyroscope or not
*/
tilt: (angle: Angle, changeScaleAndShadow?: boolean, gyro?: boolean) => void;
/**
* Resets the component (rotation/scale and glare effects)
*/
reset: () => void;
/**
* Returns the current tilt angle ({angleX: number, angleY: number})
*/
angle: () => Angle;
/**
* Adds/Removes the `will-change` CSS property to the tilt and glare elements
*
* @note Can improve performance when doing a series of animations using the `TiltRef.tilt()` function
*
* Call with `true` (default) to add the property before starting your animations and with `false` to remove it at the end
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/will-change MDN - will-change}
*
* @param {boolean} [add=true] - Whether to add the property (`true`) or remove it (`false`)
*/
updateWillChange: (add?: boolean) => void;
/**
* The component's main container element
*/
element: HTMLDivElement | null;
}
// props
export interface TiltProps extends HTMLAttributes<HTMLDivElement> {
/**
* Width of the component
*
* @note You can also set the width using `className`, `style`, etc. instead of using this property
*
* @default undefined
*
* @example 100, '200px', '10rem', '20%'
*/
width?: string | number;
/**
* Height of the component
*
* @note You can also set the height using `className`, `style`, etc. instead of using this property
*
* @default undefined
*
* @example 100, '200px', '10rem', '20%'
*/
height?: string | number;
/**
* Border radius of the component (applied to glare elements as well)
*
* @default undefined
*
* @example '4px', '1em', '2rem'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius MDN - border-radius}
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--border-radius Storybook}
*/
borderRadius?: string;
/**
* Determines how far the elements are from the user
*
* @default '1000px'
*
* @example '1000px', '60em', '50rem'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/perspective MDN - perspective}
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--perspective Storybook}
*/
perspective?: string;
/**
* Amount of scale applied to the component on hover/touch
*
* @default 1
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--scale Storybook}
*/
scale?: number;
/**
* Enables/Disables the shadow applied to the container or tilt element on hover/touch
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--shadow Storybook}
*/
shadowEnable?: boolean;
/**
* The shadow applied to the container or tilt element on hover/touch
*
* @default '0 0 1rem rgba(0,0,0,0.5)'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/drop-shadow#syntax MDN - drop-shadow}
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--shadow Storybook}
*/
shadow?: string;
/**
* Type of the shadow applied on hover/touch
*
* If set to `'box'`, shadow is applied as `box-shadow` to the tilt element
*
* If set to `'drop'`, shadow is applied as `filter: drop-shadow()` to the container element
*
* @note Set to `'drop'` if you have a setup where elements go outside the tilt element and want to apply the shadow to them as well,
*
* Or if you have multiple elements inside the tilt element and want the shadow to apply to them individually and not the whole tilt element
*
* @default 'box'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--shadow Storybook}
*/
shadowType?: ShadowType;
/**
* Enables/Disables the line glare effect
*
* @default true
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareEnable?: boolean;
/**
* Enables/Disables the blur applied to the line glare effect
*
* @default true
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareBlurEnable?: boolean;
/**
* Amount of blur applied to the line glare effect
*
* @default '4px'
*
* @eample '4px', '1em', '2rem'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareBlurAmount?: string;
/**
* Width of the line glare effect in relation to the component between `0` to `50`
*
* @default 10
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareWidthPercent?: number;
/**
* Maximum opacity of the line glare effect
*
* @default 0.1
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareMaxOpacity?: number;
/**
* `mix-blend-mode` applied to the line glare effect
*
* @note Using a `mix-blend-mode` other than `normal` will break the parallax effect (`transform-style: preserve-3d`)
*
* @default 'normal'
*
* @example 'normal', 'overlay', 'exclusion'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode MDN - mix-blend-mode}
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareMixBlendMode?: Property.MixBlendMode;
/**
* Color of the line glare effect
*
* @default 'white'
*
* @example 'lightblue', '#445566AA', 'rgba(50,150,250,0.5)', 'hsla(100,50%,50%,0.2)'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareColor?: Property.Color;
/**
* Reverses the movement of the line glare effect
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareReverse?: boolean;
/**
* Changes the direction/angle of the line glare effect
*
* @default 'to-bottom-right'
*
* @example 'to-bottom-right', 'to-bottom-left'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareDirection?: LineGlareDirection;
/**
* Determines the areas of the component that show the line glare effect when hovered/touched
*
* @default 'top-left'
*
* @example 'top-left', 'top-right', 'bottom-left', 'bottom-right'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--line-glare Storybook}
*/
lineGlareHoverPosition?: LineGlareHoverPosition;
/**
* Enables/Disables the spot glare effect
*
* @default true
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlareEnable?: boolean;
/**
* Size of the spot glare effect in relation to the component between `0` to `Infinity`
*
* @note If `spotGlarePosition` is set to anything other than `'all'`, only half of the spot glare effect is visible at any time.
*
* That's why the default value is 200 to cover the whole element.
*
* @default 200
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlareSizePercent?: number;
/**
* Maximum opacity of the spot glare effect
*
* @default 0.5
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlareMaxOpacity?: number;
/**
* `mix-blend-mode` applied to the spot glare effect
*
* @note Using a `mix-blend-mode` other than `normal` will break the parallax effect (`transform-style: preserve-3d`)
*
* @default 'normal'
*
* @example 'normal', 'overlay', 'exclusion'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode MDN - mix-blend-mode}
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlareMixBlendMode?: Property.MixBlendMode;
/**
* Determines the position of the spot glare effect inside the component
*
* @default 'top'
*
* @example 'top', 'bottom', 'left', 'right', 'all'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlarePosition?: SpotGlarePosition;
/**
* Color of the line glare effect
*
* @default 'white'
*
* @example 'lightblue', '#445566AA', 'rgba(50,150,250,0.5)', 'hsla(100,50%,50%,0.2)'
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlareColor?: Property.Color;
/**
* Reverses the movement of the spot glare effect
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--spot-glare Storybook}
*/
spotGlareReverse?: boolean;
/**
* Maximum tilt angle around the `X` axis between `0` to `90`
*
* @note Setting to `0` will disable rotation around the `X` axis
*
* @default 20
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--tilt-max-angle Storybook}
*/
tiltMaxAngleX?: number;
/**
* Maximum tilt angle around the `Y` axis between `0` to `90`
*
* @note Setting to `0` will disable rotation around the `Y` axis
*
* @default 20
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--tilt-max-angle Storybook}
*/
tiltMaxAngleY?: number;
/**
* Reverses the tilt direction/movement
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--tilt-reverse Storybook}
*/
tiltReverse?: boolean;
/**
* Enables/Disables resetting the tilt effect on mouseLeave/touchEnd
*
* @default true
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--tilt-reset Storybook}
*/
tiltReset?: boolean;
/**
* Initial tilt/rotation angle around the `X` axis
*
* @note Is limited to the `[-tiltMaxAngleX - tiltMaxAngleX]` range
*
* @default 0
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--initial-angle Storybook}
*/
initialAngleX?: number;
/**
* Initial tilt/rotation angle around the `Y` axis
*
* @note Is limited to the `[-tiltMaxAngleY - tiltMaxAngleY]` range
*
* @default 0
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--initial-angle Storybook}
*/
initialAngleY?: number;
/**
* Disables scrolling (`overflow: hidden`) during touch inetraction to prevent unwanted movement
*
* @note Disables scrolling on `body` if set to `boolean`
*
* You can also pass an `HTMLElement` which scrolling will be disabled for, instead of `body`
*
* This property will have no effect if `fullPageListening` is set to `true`
*
* @default true
*/
disableScrollOnTouch?: boolean | HTMLElement;
/**
* Style passed to the component's container element
*
* @default undefined
*/
style?: CSSProperties;
/**
* Style passed to the component's tilt element
*
* @default undefined
*/
tiltStyle?: CSSProperties;
/**
* Properties passed to the tilt element
*
* @example className='...' data-...='...'
*
* @default undefined
*/
tiltProps?: HTMLAttributes<HTMLDivElement> & {
[data: `data-${string}`]: string | undefined;
};
/**
* Maximum tilt angle around the `X` axis for gyroscope between `0` to `90`
*
* @note Setting to `0` will disable rotation around the `X` axis for gyroscope
*
* Gyroscope detection (device orientation event) only works in `HTTPS`
*
* @default 0
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--gyro-max-angle Storybook}
*/
gyroMaxAngleX?: number;
/**
* Maximum tilt angle around the `Y` axis for gyroscope between `0` to `90`
*
* @note Setting to `0` will disable rotation around the `Y` axis for gyroscope
*
* Gyroscope detection (device orientation event) only works in `HTTPS`
*
* @default 0
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--gyro-max-angle Storybook}
*/
gyroMaxAngleY?: number;
/**
* Reverses the tilt direction for gyroscope
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--gyro-reverse Storybook}
*/
gyroReverse?: boolean;
/**
* Disables the tilt effect and applies the disabledFilter to the container
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--disabled Storybook}
*/
disabled?: boolean;
/**
* CSS filter applied to the container when `disabled = true`
*
* @default 'grayscale(1) brightness(125%)'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/filter MDN - filter}
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt/?path=/story/tilt-react-next-tilt--disabled Storybook}
*/
disabledFilter?: string;
/**
* CSS transition applied to the tilt, line glare, and spot glare elements
*
* @default 'all 0.4s cubic-bezier(0.03, 0.98, 0.52, 0.99)'
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transition MDN - transition}
*/
CSSTransition?: string;
/**
* The Component wrapping the tilt element
*
* @note Is useful when integrating this component into another component
*
* @default ({ children }: PropsWithChildren) => <>{children}</>
*/
TiltWrapper?: FC<PropsWithChildren>;
/**
* Enables/Disables full-page listening. This component's event handlers will be added to the `document`
*
* @note If set to `true`, `controlElement`, `controlElementOnly`, and `disableScrollOnTouch` properties will have no effect
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt_demo/control-element Control Element Demo}
*/
fullPageListening?: boolean;
/**
* Element(s) that control(s) this component. This component's event handlers will be added to them
*
* @note You can pass an HTMLElement, a ref, or an array of them
*
* This property will have no effect if `fullPageListening` is set to `true`
*
* @default undefined
*
* @example element, ref, [element, ref], [ref1, ref2]
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt_demo/control-element Control Element Demo}
*/
controlElement?:
| HTMLElement
| RefObject<unknown>
| Array<HTMLElement | RefObject<unknown>>;
/**
* If set to `true`, events will be disabled for the component and it will be controlled by the controlElement(s) only
*
* @note This property will have no effect if `fullPageListening` is set to `true`
*
* @default false
*
* @see {@link https://rashidshamloo.github.io/react-next-tilt_demo/control-element Control Element Demo}
*/
controlElementOnly?: boolean;
/**
* If set to true, adds `transform-style: preserve-3d;` to the container and tilt elements
*
* @note Enable if you want to set up a parallax effect and translate elements along the `Z` axis
*
* Disable if you are having problems with blur
*
* @warning Can cause blur on scale (prevents re-rastering at higher scales by Chrome's compositor and the element is always rasterized at scale 1)
*
* @default true
*/
preserve3dEnable?: boolean;
/**
* Adds the `data-testid=...` property to all elements for testing purposes
*
* @note Can also be used to select/grab and modify each element if you want to do heavy customization
*
* @default false
*/
testIdEnable?: boolean;
/**
* Callback function that is called with the current tilt angle at every tilt event
*
* @param {Angle} angle - Tilt angle ({angleX: number, angleY: number})
* @param {boolean} gyro - Whether the event is triggered by gyroscope or not
*
* @default undefined
*/
onTilt?: (angle: Angle, gyro: boolean) => void;
/**
* Callback function that is called when the tilt angle is reset
*
* @default undefined
*/
onReset?: () => void;
[data: `data-${string}`]: string | undefined;
onMouseEnter?: MouseEventHandler<HTMLDivElement>;
onMouseMove?: MouseEventHandler<HTMLDivElement>;
onMouseLeave?: MouseEventHandler<HTMLDivElement>;
onTouchStart?: TouchEventHandler<HTMLDivElement>;
onTouchMove?: TouchEventHandler<HTMLDivElement>;
onTouchEnd?: TouchEventHandler<HTMLDivElement>;
onBlur?: FocusEventHandler<HTMLDivElement>;
}