@behivetech/molecules.file-dropzone 0.2.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ import {
2
+ pressHandlers,
3
+ useBlock,
4
+ type StylesFor,
5
+ } from "@behivetech/blocks.core/native";
6
+ import { getDocumentAsync } from "expo-document-picker";
7
+ import { useRef, useState, type ReactNode } from "react";
8
+ import {
9
+ Pressable,
10
+ Text,
11
+ type PressableProps,
12
+ type StyleProp,
13
+ type ViewStyle,
14
+ } from "react-native";
15
+ import { fileDropzone } from "./file-dropzone.block";
16
+
17
+ /** A file the picker returned. Read it from `uri` (with `expo-file-system`, or upload it from there). */
18
+ export interface PickedFile {
19
+ /** Where the file is, as a `file://` address in the app's cache */
20
+ uri: string;
21
+ /** The file's name, with its extension */
22
+ name: string;
23
+ /** Size in bytes, when the platform says */
24
+ size?: number;
25
+ /** MIME type, when the platform says */
26
+ mimeType?: string;
27
+ }
28
+
29
+ export interface FileDropzoneProps extends Omit<
30
+ PressableProps,
31
+ "style" | "children" | "onPress" | "disabled"
32
+ > {
33
+ /** Called with the chosen files; not called when the person cancels the picker */
34
+ onFiles: (files: PickedFile[]) => void;
35
+ /** Called when the picker fails to open or to read a file */
36
+ onError?: (error: unknown) => void;
37
+ /** MIME types the picker offers, separated by commas, e.g. "image/*,application/pdf"; the web's file extensions (`.pdf`) are ignored. Omit for any file. */
38
+ accept?: string;
39
+ /** Whether more than one file can be selected at once; defaults to true */
40
+ multiple?: boolean;
41
+ /** Prompt content; defaults to "Tap to choose files". The label is drawn inside a `Text`; a text label names the button for a screen reader unless you pass an `accessibilityLabel`. */
42
+ label?: ReactNode;
43
+ /** Additional styles, appended after the block's own */
44
+ style?: StyleProp<ViewStyle>;
45
+ /** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
46
+ styles?: StylesFor<typeof fileDropzone>;
47
+ }
48
+
49
+ const DEFAULT_LABEL = "Tap to choose files";
50
+ const ANY_FILE = "*/*";
51
+
52
+ /** The MIME types of an `accept` string: the entries that name a type (not a `.ext`), or any file when none do. */
53
+ const getMimeTypes = (accept: string | undefined): string[] => {
54
+ const types = (accept ?? "")
55
+ .split(",")
56
+ .map((entry) => entry.trim())
57
+ .filter((entry) => entry.includes("/"));
58
+
59
+ return types.length > 0 ? types : [ANY_FILE];
60
+ };
61
+
62
+ /**
63
+ * The React Native renderer of FileDropzone: the same block as the web one, as a button that opens the
64
+ * device's file picker (`expo-document-picker`, an optional peer dependency: an Expo app lists it, `npx expo
65
+ * install expo-document-picker`). There is no drag and drop on a phone. `onFiles` receives `PickedFile`s (a
66
+ * `uri`, not the web's `File` objects), and `accept` takes MIME types only. The picker opens once at a time.
67
+ * Your press, hover and focus handlers are called after the block's own, and a `hitSlop` you pass is the button's.
68
+ */
69
+ export const FileDropzone = ({
70
+ onFiles,
71
+ onError,
72
+ accept,
73
+ multiple = true,
74
+ label = DEFAULT_LABEL,
75
+ style,
76
+ styles,
77
+ ...rest
78
+ }: FileDropzoneProps) => {
79
+ const [isPicking, setIsPicking] = useState(false);
80
+ const [rootProps, childProps] = useBlock(
81
+ fileDropzone,
82
+ {},
83
+ {
84
+ style,
85
+ styles,
86
+ // The block's own handlers track its states; the consumer's are called after them, not replaced by the spread.
87
+ handlers: pressHandlers(rest),
88
+ hitSlop: rest.hitSlop,
89
+ },
90
+ );
91
+ const isWords = typeof label === "string" || typeof label === "number";
92
+
93
+ // a ref, not the state alone: two presses in one frame both see the state as not busy
94
+ const picking = useRef(false);
95
+
96
+ const pick = async () => {
97
+ if (picking.current) {
98
+ return;
99
+ }
100
+ picking.current = true;
101
+ setIsPicking(true);
102
+ let files: PickedFile[] = [];
103
+
104
+ try {
105
+ const result = await getDocumentAsync({
106
+ type: getMimeTypes(accept),
107
+ multiple,
108
+ copyToCacheDirectory: true,
109
+ });
110
+
111
+ if (!result.canceled) {
112
+ files = result.assets.map(({ uri, name, size, mimeType }) => ({
113
+ uri,
114
+ name,
115
+ size,
116
+ mimeType,
117
+ }));
118
+ }
119
+ } catch (error) {
120
+ onError?.(error);
121
+ } finally {
122
+ picking.current = false;
123
+ setIsPicking(false);
124
+ }
125
+ // outside the try: an error thrown by the caller's handler is the caller's, not a picker failure
126
+ if (files.length > 0) {
127
+ onFiles(files);
128
+ }
129
+ };
130
+
131
+ return (
132
+ <Pressable
133
+ accessibilityLabel={isWords ? String(label) : undefined}
134
+ {...rest}
135
+ {...rootProps}
136
+ role="button"
137
+ accessibilityState={{ busy: isPicking }}
138
+ disabled={isPicking}
139
+ onPress={() => void pick()}
140
+ >
141
+ <Text {...childProps("label")}>{label}</Text>
142
+ </Pressable>
143
+ );
144
+ };
package/file-dropzone.tsx CHANGED
@@ -1,6 +1,8 @@
1
1
  import { useRef, useState, type DragEvent, type ReactNode } from "react";
2
- import { getClassName } from "@behivetech/get-class-name";
3
- import styles from "./file-dropzone.module.scss";
2
+
3
+ import "./file-dropzone.block.css";
4
+ import { useBlock, type StylesFor } from "@behivetech/blocks.core";
5
+ import { fileDropzone } from "./file-dropzone.block";
4
6
 
5
7
  export interface FileDropzoneProps {
6
8
  /** Called with the chosen files, from either a drop or the click-to-browse fallback */
@@ -13,6 +15,9 @@ export interface FileDropzoneProps {
13
15
  label?: ReactNode;
14
16
  /** Additional class names to merge with the component root element */
15
17
  className?: string;
18
+
19
+ /** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
20
+ styles?: StylesFor<typeof fileDropzone>;
16
21
  }
17
22
 
18
23
  /**
@@ -26,16 +31,16 @@ export const FileDropzone = ({
26
31
  multiple = true,
27
32
  label = "Drag and drop files here, or click to browse",
28
33
  className,
34
+ styles,
29
35
  }: FileDropzoneProps) => {
30
36
  const [isDragging, setIsDragging] = useState(false);
31
37
  const inputRef = useRef<HTMLInputElement>(null);
32
38
 
33
- const [rootClass] = getClassName({
34
- className,
35
- rootClass: "BHT__FileDropzone",
36
- modifiers: { dragging: isDragging },
37
- styles,
38
- });
39
+ const [rootProps] = useBlock(
40
+ fileDropzone,
41
+ { dragging: isDragging },
42
+ { className, styles },
43
+ );
39
44
 
40
45
  const handleDragOver = (event: DragEvent<HTMLDivElement>) => {
41
46
  event.preventDefault();
@@ -55,7 +60,7 @@ export const FileDropzone = ({
55
60
 
56
61
  return (
57
62
  <div
58
- className={rootClass}
63
+ {...rootProps}
59
64
  role="button"
60
65
  tabIndex={0}
61
66
  onClick={() => inputRef.current?.click()}
package/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { FileDropzone } from "./file-dropzone.js";
2
- export type { FileDropzoneProps } from "./file-dropzone.js";
1
+ export { FileDropzone } from "./file-dropzone";
2
+ export type { FileDropzoneProps } from "./file-dropzone";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@behivetech/molecules.file-dropzone",
3
- "version": "0.2.5",
3
+ "version": "1.1.0",
4
4
  "license": "MIT",
5
5
  "description": "A drag-and-drop target with a click-to-browse fallback that reports the chosen `File` objects through `onFiles` — no upload, preview or validation of its own.",
6
6
  "type": "module",
@@ -10,18 +10,26 @@
10
10
  ],
11
11
  "exports": {
12
12
  ".": {
13
+ "react-native": {
14
+ "types": "./dist/native/index.d.ts",
15
+ "default": "./dist/index.native.js"
16
+ },
13
17
  "types": "./dist/index.d.ts",
14
18
  "import": "./dist/index.js",
15
19
  "require": "./dist/index.cjs"
16
20
  },
17
21
  "./styles.css": "./dist/index.css"
18
22
  },
23
+ "react-native": "./dist/index.native.js",
19
24
  "files": [
20
25
  "dist",
21
26
  "README.md",
22
27
  "index.ts",
23
28
  "file-dropzone.tsx",
24
- "file-dropzone.module.scss",
29
+ "file-dropzone.block.ts",
30
+ "file-dropzone.block.css",
31
+ "file-dropzone.native.tsx",
32
+ "file-dropzone.block.native.ts",
25
33
  "*.example.tsx",
26
34
  "docs.json"
27
35
  ],
@@ -30,17 +38,31 @@
30
38
  "tags": []
31
39
  },
32
40
  "dependencies": {
33
- "@behivetech/get-class-name": "0.1.0"
41
+ "@behivetech/blocks.core": "0.1.0"
34
42
  },
35
43
  "peerDependencies": {
36
44
  "react": "^19.2.7",
37
- "react-dom": "^19.2.7"
45
+ "react-dom": "^19.2.7",
46
+ "react-native": "*",
47
+ "expo-document-picker": "*"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "expo-document-picker": {
51
+ "optional": true
52
+ },
53
+ "react-native": {
54
+ "optional": true
55
+ }
38
56
  },
39
57
  "devDependencies": {
40
- "@bht-component-registry/env": "0.1.0",
58
+ "@behivetech/blocks.builder": "0.1.0",
59
+ "@behivetech/blocks.native-test": "0.0.1",
60
+ "@bht-component-registry/env": "0.3.0",
41
61
  "@types/react": "19.2.17",
42
62
  "@types/react-dom": "19.2.3",
43
63
  "eslint": "^9.39.5",
64
+ "react": "19.2.8",
65
+ "react-dom": "19.2.8",
44
66
  "tsup": "^8.5.1",
45
67
  "typescript": "^6.0.3",
46
68
  "vitest": "^4.1.10"
@@ -48,15 +70,26 @@
48
70
  "repository": {
49
71
  "type": "git",
50
72
  "url": "https://github.com/behivetech/bht-galaxy.git",
51
- "directory": "registry/behivetech/molecules/file-dropzone"
73
+ "directory": "packages/molecules/file-dropzone"
52
74
  },
53
75
  "homepage": "https://platform.behivetech.com/behivetech/molecules/file-dropzone",
54
76
  "publishConfig": {
55
- "registry": "https://npm.pkg.github.com",
77
+ "registry": "https://registry.npmjs.org",
56
78
  "access": "public"
57
79
  },
80
+ "blocks": {
81
+ "a11y": {
82
+ "surface": [
83
+ "background",
84
+ "surface"
85
+ ],
86
+ "elementRole": [
87
+ "BHT__FileDropzone"
88
+ ]
89
+ }
90
+ },
58
91
  "scripts": {
59
- "build": "tsup && bht-registry-docgen",
92
+ "build": "block check && tsup && bht-registry-docgen",
60
93
  "dev": "tsup --watch",
61
94
  "lint": "eslint . --max-warnings 0",
62
95
  "check-types": "tsc --noEmit",
@@ -1,69 +0,0 @@
1
- // Drop target. MD3 has no dropzone component, so it borrows the outlined
2
- // container (dashed `outline` border, medium corner) and expresses hover and
3
- // drag-over through the standard state layer instead of a fixed background.
4
-
5
- .BHT__FileDropzone {
6
- // `isolation` keeps the state layer's stacking context local to the zone.
7
- position: relative;
8
- isolation: isolate;
9
- display: flex;
10
- align-items: center;
11
- justify-content: center;
12
- text-align: center;
13
- min-height: 8rem;
14
- padding: var(--spacing-6, 1.5rem);
15
- border: 2px dashed var(--md-sys-color-outline, var(--color-border-strong));
16
- border-radius: var(--md-sys-shape-corner-medium, 12px);
17
- color: var(--md-sys-color-on-surface-variant, var(--color-text-muted));
18
- font-family: var(--md-sys-typescale-body-medium-font, var(--font-body));
19
- font-size: var(--md-sys-typescale-body-medium-size, 0.875rem);
20
- font-weight: var(--md-sys-typescale-body-medium-weight, 400);
21
- line-height: var(--md-sys-typescale-body-medium-line-height, 1.25rem);
22
- letter-spacing: var(--md-sys-typescale-body-medium-tracking, 0.0179em);
23
- cursor: pointer;
24
- transition: border-color var(--md-sys-motion-duration-short-3, 150ms)
25
- var(--md-sys-motion-easing-standard, ease);
26
-
27
- &::after {
28
- content: "";
29
- position: absolute;
30
- inset: 0;
31
- border-radius: inherit;
32
- background: currentColor;
33
- opacity: 0;
34
- pointer-events: none;
35
- transition: opacity var(--md-sys-motion-duration-short-2, 100ms)
36
- var(--md-sys-motion-easing-standard, ease);
37
- }
38
-
39
- &:hover {
40
- border-color: var(--md-sys-color-primary, var(--color-primary));
41
- }
42
-
43
- &:hover::after {
44
- opacity: var(--md-sys-state-hover-opacity, 0.08);
45
- }
46
-
47
- &:focus-visible {
48
- outline: 3px solid var(--md-sys-color-secondary, var(--color-border-focus));
49
- outline-offset: 2px;
50
- }
51
-
52
- &:focus-visible::after {
53
- opacity: var(--md-sys-state-focus-opacity, 0.1);
54
- }
55
-
56
- &:active::after {
57
- opacity: var(--md-sys-state-pressed-opacity, 0.1);
58
- }
59
-
60
- &--dragging {
61
- &.BHT__FileDropzone {
62
- border-color: var(--md-sys-color-primary, var(--color-primary));
63
-
64
- &::after {
65
- opacity: var(--md-sys-state-dragged-opacity, 0.16);
66
- }
67
- }
68
- }
69
- }