Skip to content

Commit 2e1266b

Browse files
authored
Merge pull request #20 from uploadcare/feature/implement-styles
feat(react-uploader): updated @uploadcare/blocks and implement styles
2 parents 9611a61 + f7b19cb commit 2e1266b

File tree

11 files changed

+69
-75
lines changed

11 files changed

+69
-75
lines changed

package-lock.json

Lines changed: 13 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-adapter/src/customElementToReactComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Options<
1010
> = {
1111
react: typeof React;
1212
tag: string;
13-
elClass: { new (): I };
13+
elClass: { new(): I };
1414
schemaEvents?: E;
1515
};
1616

@@ -54,7 +54,7 @@ export const customElementToReactComponent = <
5454
Record<string, unknown>,
5555
Set<string>,
5656
I
57-
// @ts-ignore
57+
// @ts-ignore
5858
>(props, eventKeyOfProps, elClass);
5959

6060
React.useLayoutEffect(() => {

packages/react-adapter/src/utils/registerPropAndEvent.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const listenedEvents = new WeakMap<Element, Map<string, EventListenerObject>>();
2+
type THandleEvent = (e?: Event) => void;
23

34
type TRegisterEvent<E> = {
45
node: E;
@@ -12,7 +13,7 @@ const ensureEventMapForNode = (
1213
node: Element,
1314
): Map<string, EventListenerObject> => {
1415
let events = listenedEvents.get(node);
15-
if (!events) {
16+
if (events === undefined) {
1617
events = new Map();
1718
listenedEvents.set(node, events);
1819
}
@@ -27,24 +28,28 @@ export const registerPropAndEvent = <E extends Element>({
2728
event,
2829
}: TRegisterEvent<E>) => {
2930
// Subscribe to the event if it is defined
30-
if (event !== undefined && valueProp !== prevValueProp) {
31-
const events = ensureEventMapForNode(node);
32-
const handlerExists = events.has(event);
31+
if (event !== undefined) {
32+
if (valueProp !== prevValueProp) {
33+
const events = ensureEventMapForNode(node);
34+
const handlerExists = events.has(event);
35+
let handler = events.get(event) as EventListenerObject;
3336

34-
if (valueProp) {
35-
const handler: EventListenerObject = {
36-
handleEvent: valueProp as (e?: Event) => void,
37-
};
38-
if (!handlerExists) {
39-
node.addEventListener(event, handler);
37+
if (valueProp !== undefined) {
38+
if (!handlerExists) {
39+
handler = { handleEvent: valueProp as THandleEvent };
40+
events.set(event, handler);
41+
// @ts-ignore
42+
node.addEventListener(event, (event: CustomEvent) =>
43+
handler.handleEvent(event.detail),
44+
);
45+
} else {
46+
handler.handleEvent = valueProp as THandleEvent;
47+
}
48+
} else if (handlerExists) {
49+
events.delete(event);
50+
node.removeEventListener(event, handler);
4051
}
41-
events.set(event, handler);
42-
} else if (handlerExists) {
43-
const handler = events.get(event);
44-
node.removeEventListener(event, handler!);
45-
events.delete(event);
4652
}
47-
4853
return;
4954
}
5055

packages/react-uploader/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@uploadcare/react-uploader",
33
"description": "React component for file uploads using Uploadcare",
4-
"version": "0.3.0",
4+
"version": "0.3.0-alpha.2",
55
"private": false,
66
"type": "module",
77
"files": [
@@ -14,7 +14,8 @@
1414
".": {
1515
"import": "./dist/react-uploader.js",
1616
"require": "./dist/react-uploader.cjs"
17-
}
17+
},
18+
"./core.css": "./dist/libs.css"
1819
},
1920
"scripts": {
2021
"dev": "tsc && vite build --watch",
@@ -26,8 +27,8 @@
2627
"@types/react": "17 || 18"
2728
},
2829
"dependencies": {
29-
"@uploadcare/blocks": "^0.36.1-alpha.1",
30-
"@uploadcare/react-adapter": "^0.0.1-alpha.1"
30+
"@uploadcare/blocks": "^0.39.1",
31+
"@uploadcare/react-adapter": "0.2.0"
3132
},
3233
"repository": {
3334
"type": "git",

packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { type FC, useMemo } from "react";
22
import * as LR from "@uploadcare/blocks";
3+
import "@uploadcare/blocks/web/lr-file-uploader-inline.min.css";
34
import { customElementToReactComponent } from "@uploadcare/react-adapter";
45
import { AdapterConfig } from "../core/AdapterConfig";
56
import { AdapterUploadCtxProvider } from "../core/AdapterUploadCtxProvider";
67
import type { TProps } from "../types";
7-
import { getStyleSource } from "../default";
88
import { getCalcPropertyOfProps } from "../../utils/getCalcPropertyOfProps";
9-
import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration.ts";
9+
import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration";
1010

1111
LR.registerBlocks(LR);
1212

@@ -16,10 +16,10 @@ const AdapterFileUploaderInline = customElementToReactComponent({
1616
elClass: LR.FileUploaderMinimal,
1717
});
1818

19-
const CSS_SRC_INLINE = getStyleSource("inline");
2019
export const FileUploaderInline: FC<TProps> = ({
2120
ctxName,
2221
className,
22+
classNameUploader,
2323
apiRef,
2424
...props
2525
}) => {
@@ -41,7 +41,8 @@ export const FileUploaderInline: FC<TProps> = ({
4141
{...eventHandlers}
4242
/>
4343

44-
<AdapterFileUploaderInline ctx-name={CTX_NAME} css-src={CSS_SRC_INLINE} />
44+
{/* @ts-ignore */}
45+
<AdapterFileUploaderInline class={classNameUploader} ctx-name={CTX_NAME} />
4546
</div>
4647
);
4748
};

packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { type FC, useMemo } from "react";
22
import * as LR from "@uploadcare/blocks";
3+
import "@uploadcare/blocks/web/lr-file-uploader-minimal.min.css"
34
import { customElementToReactComponent } from "@uploadcare/react-adapter";
45
import { AdapterConfig } from "../core/AdapterConfig";
56
import { AdapterUploadCtxProvider } from "../core/AdapterUploadCtxProvider";
67
import type { TProps } from "../types";
7-
import { getStyleSource } from "../default";
88
import { getCalcPropertyOfProps } from "../../utils/getCalcPropertyOfProps";
9-
import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration.ts";
9+
import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration";
1010

1111
LR.registerBlocks(LR);
1212

@@ -16,11 +16,11 @@ const AdapterFileUploaderMinimal = customElementToReactComponent({
1616
elClass: LR.FileUploaderMinimal,
1717
});
1818

19-
const CSS_SRC_MINIMAL = getStyleSource("minimal");
2019

2120
export const FileUploaderMinimal: FC<TProps> = ({
2221
ctxName,
2322
className,
23+
classNameUploader,
2424
apiRef,
2525
...props
2626
}) => {
@@ -41,10 +41,8 @@ export const FileUploaderMinimal: FC<TProps> = ({
4141
ctx-name={CTX_NAME}
4242
{...eventHandlers}
4343
/>
44-
<AdapterFileUploaderMinimal
45-
ctx-name={CTX_NAME}
46-
css-src={CSS_SRC_MINIMAL}
47-
/>
44+
{/* @ts-ignore */}
45+
<AdapterFileUploaderMinimal class={classNameUploader} ctx-name={CTX_NAME} />
4846
</div>
4947
);
5048
};

packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import * as LR from "@uploadcare/blocks";
33
import { customElementToReactComponent } from "@uploadcare/react-adapter";
44
import { AdapterConfig } from "../core/AdapterConfig";
55
import { AdapterUploadCtxProvider } from "../core/AdapterUploadCtxProvider";
6-
import { getStyleSource } from "../default";
76
import type { TProps } from "../types";
87

98
import { getCalcPropertyOfProps } from "../../utils/getCalcPropertyOfProps";
10-
import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration.ts";
9+
import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration";
1110

1211
LR.registerBlocks(LR);
1312

@@ -17,11 +16,10 @@ const AdapterFileUploaderRegular = customElementToReactComponent({
1716
elClass: LR.FileUploaderRegular,
1817
});
1918

20-
const CSS_SRC_REGULAR = getStyleSource("regular");
21-
2219
export const FileUploaderRegular: FC<TProps> = ({
2320
ctxName,
2421
className,
22+
classNameUploader,
2523
apiRef,
2624
...props
2725
}) => {
@@ -42,10 +40,8 @@ export const FileUploaderRegular: FC<TProps> = ({
4240
ctx-name={CTX_NAME}
4341
{...eventHandlers}
4442
/>
45-
<AdapterFileUploaderRegular
46-
ctx-name={CTX_NAME}
47-
css-src={CSS_SRC_REGULAR}
48-
/>
43+
{/* @ts-ignore */}
44+
<AdapterFileUploaderRegular class={classNameUploader} ctx-name={CTX_NAME} />
4945
</div>
5046
);
5147
};

packages/react-uploader/src/Uploader/default.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/react-uploader/src/Uploader/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ type TExtraPrefixOn<S extends string> = `on${Capitalize<S>}`;
1414
type TPrefixOnAndCamelCase<S extends string> = TExtraPrefixOn<TToCamelCase<S>>;
1515

1616
export type TEventsSchema = {
17-
[K in keyof EventMap as TPrefixOnAndCamelCase<K>]: EventMap[K];
17+
[K in keyof EventMap as TPrefixOnAndCamelCase<K>]: (
18+
event: EventMap[K]["detail"],
19+
) => void;
1820
};
1921

2022
type TRefUploadCtxProvider = {
@@ -27,6 +29,7 @@ type TPropsWithConfig = Partial<ConfigType>;
2729

2830
type TDefaultProps = {
2931
className?: string;
32+
classNameUploader?: string;
3033
ctxName?: string;
3134
};
3235

packages/react-uploader/src/libs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ export { FileUploaderMinimal } from "./Uploader/Minimal/FileUploaderMinimal";
33
export { FileUploaderInline } from "./Uploader/Inline/FileUploaderInline";
44

55
export { type TProps, UploadCtxProvider } from "./Uploader/types";
6+
export { type TEventsSchema } from './Uploader/types'
7+
8+
export { defineLocale } from "@uploadcare/blocks"

0 commit comments

Comments
 (0)