-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathDropZone.tsx
More file actions
181 lines (171 loc) · 6.03 KB
/
Copy pathDropZone.tsx
File metadata and controls
181 lines (171 loc) · 6.03 KB
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
/*
* Copyright 2023 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 {AriaLabelingProps, GlobalDOMAttributes, HoverEvents} from '@react-types/shared';
import {
ClassNameOrFunction,
ContextValue,
dom,
Provider,
RenderProps,
SlotProps,
useContextProps,
useRenderProps
} from './utils';
import {DropOptions, useDrop} from 'react-aria/useDrop';
import {filterDOMProps} from 'react-aria/filterDOMProps';
import {getEventTarget, nodeContains} from 'react-aria/private/utils/shadowdom/DOMFunctions';
import intlMessages from '../intl/*.json';
import {isFocusable} from 'react-aria/private/utils/isFocusable';
import {mergeProps} from 'react-aria/mergeProps';
import React, {createContext, CSSProperties, ForwardedRef, forwardRef, useRef} from 'react';
import {TextContext} from './Text';
import {useButton} from 'react-aria/useButton';
import {useClipboard} from 'react-aria/useClipboard';
import {useFocusRing} from 'react-aria/useFocusRing';
import {useHover} from 'react-aria/useHover';
import {useLabels} from 'react-aria/private/utils/useLabels';
import {useLocalizedStringFormatter} from 'react-aria/useLocalizedStringFormatter';
import {useObjectRef} from 'react-aria/useObjectRef';
import {useSlotId} from 'react-aria/private/utils/useId';
import {VisuallyHidden} from 'react-aria/VisuallyHidden';
export interface DropZoneRenderProps {
/**
* Whether the dropzone is currently hovered with a mouse.
*
* @selector [data-hovered]
*/
isHovered: boolean;
/**
* Whether the dropzone is focused, either via a mouse or keyboard.
*
* @selector [data-focused]
*/
isFocused: boolean;
/**
* Whether the dropzone is keyboard focused.
*
* @selector [data-focus-visible]
*/
isFocusVisible: boolean;
/**
* Whether the dropzone is the drop target.
*
* @selector [data-drop-target]
*/
isDropTarget: boolean;
/**
* Whether the dropzone is disabled.
*
* @selector [data-disabled]
*/
isDisabled: boolean;
}
export interface DropZoneProps
extends
Omit<DropOptions, 'getDropOperationForPoint' | 'ref' | 'hasDropButton'>,
HoverEvents,
RenderProps<DropZoneRenderProps>,
SlotProps,
AriaLabelingProps,
GlobalDOMAttributes<HTMLDivElement> {
/**
* The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the
* element. A function may be provided to compute the class based on component state.
*
* @default 'react-aria-DropZone'
*/
className?: ClassNameOrFunction<DropZoneRenderProps>;
/**
* The inline style for the visually hidden drop button used for keyboard and screen reader drop
* interactions.
*/
dropButtonStyle?: CSSProperties;
}
export const DropZoneContext = createContext<ContextValue<DropZoneProps, HTMLDivElement>>(null);
/**
* A drop zone is an area into which one or multiple objects can be dragged and dropped.
*/
export const DropZone = forwardRef(function DropZone(
props: DropZoneProps,
ref: ForwardedRef<HTMLDivElement>
) {
let {isDisabled = false} = props;
[props, ref] = useContextProps(props, ref, DropZoneContext);
let {dropButtonStyle} = props;
let dropzoneRef = useObjectRef(ref);
let buttonRef = useRef<HTMLButtonElement>(null);
let {dropProps, dropButtonProps, isDropTarget} = useDrop({
...props,
ref: buttonRef,
hasDropButton: true
});
let {buttonProps} = useButton(dropButtonProps || {}, buttonRef);
let {hoverProps, isHovered} = useHover(props);
let {focusProps, isFocused, isFocusVisible} = useFocusRing();
let stringFormatter = useLocalizedStringFormatter(intlMessages, 'react-aria-components');
let textId = useSlotId();
let ariaLabel = props['aria-label'] || stringFormatter.format('dropzoneLabel');
let messageId = props['aria-labelledby'];
let ariaLabelledby = [textId, messageId].filter(Boolean).join(' ');
let labelProps = useLabels({'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby});
let {clipboardProps} = useClipboard({
isDisabled,
onPaste: items =>
props.onDrop?.({
type: 'drop',
items,
x: 0,
y: 0,
dropOperation: 'copy'
})
});
let renderProps = useRenderProps({
...props,
values: {isHovered, isFocused, isFocusVisible, isDropTarget, isDisabled},
defaultClassName: 'react-aria-DropZone'
});
let DOMProps = filterDOMProps(props, {global: true});
delete DOMProps.id;
return (
<Provider values={[[TextContext, {id: textId, slot: 'label'}]]}>
<dom.div
{...mergeProps(DOMProps, renderProps, dropProps, hoverProps)}
slot={props.slot || undefined}
ref={dropzoneRef}
onClick={e => {
let target = getEventTarget(e) as HTMLElement | null;
while (target && nodeContains(dropzoneRef.current, target)) {
if (isFocusable(target)) {
break;
} else if (target === dropzoneRef.current) {
buttonRef.current?.focus();
break;
}
target = target.parentElement;
}
}}
data-hovered={isHovered || undefined}
data-focused={isFocused || undefined}
data-focus-visible={isFocusVisible || undefined}
data-drop-target={isDropTarget || undefined}
data-disabled={isDisabled || undefined}>
<VisuallyHidden style={dropButtonStyle}>
<button
{...mergeProps(buttonProps, focusProps, clipboardProps, labelProps)}
ref={buttonRef}
/>
</VisuallyHidden>
{renderProps.children}
</dom.div>
</Provider>
);
});