@bbki.ng/components 1.5.2 → 1.5.3

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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React, { EventHandler, ReactElement, CSSProperties } from 'react';
1
+ import React, { EventHandler, ReactElement, CSSProperties, FunctionComponent } from 'react';
2
2
  import { LinkProps as LinkProps$1 } from 'react-router-dom';
3
3
 
4
4
  declare type ArticleProps = {
@@ -123,4 +123,16 @@ declare type SkeletonProps = {
123
123
  };
124
124
  declare const Skeleton: (props: SkeletonProps) => JSX.Element;
125
125
 
126
- export { Article, ArticleProps, Breadcrumb, BreadcrumbProps, Button, ButtonProps, ButtonType, Link, LinkColor, LinkProps, Logo, LogoProps, Nav, NavProps, Page, Panel, PanelProps, PathObj, PopConfirm, PopConfirmProps, Skeleton, SkeletonColor, SkeletonProps, Table, TableProps, Tag, TagProps, Tags };
126
+ interface ImageDropProps<T> {
127
+ uploader: (file: File, img: HTMLImageElement) => Promise<T>;
128
+ onDrop?: (events: Event, file: File) => void;
129
+ onUploadFinish?: (result: T) => void;
130
+ waitTimeAfterFinish?: number;
131
+ defaultBgColor?: string;
132
+ dragOverBgColor?: string;
133
+ dropAreaStyle?: CSSStyleDeclaration;
134
+ placeholder?: any;
135
+ }
136
+ declare const DropImage: FunctionComponent<ImageDropProps<any>>;
137
+
138
+ export { Article, ArticleProps, Breadcrumb, BreadcrumbProps, Button, ButtonProps, ButtonType, DropImage, ImageDropProps, Link, LinkColor, LinkProps, Logo, LogoProps, Nav, NavProps, Page, Panel, PanelProps, PathObj, PopConfirm, PopConfirmProps, Skeleton, SkeletonColor, SkeletonProps, Table, TableProps, Tag, TagProps, Tags };
package/dist/index.js CHANGED
@@ -62,6 +62,7 @@ __export(src_exports, {
62
62
  Breadcrumb: () => Breadcrumb,
63
63
  Button: () => Button,
64
64
  ButtonType: () => ButtonType,
65
+ DropImage: () => DropImage,
65
66
  Link: () => Link,
66
67
  LinkColor: () => LinkColor,
67
68
  Logo: () => Logo,
@@ -348,6 +349,170 @@ var Skeleton = (props) => {
348
349
  style: { width, height }
349
350
  });
350
351
  };
352
+
353
+ // src/drop-image/DropImage.tsx
354
+ var import_classnames6 = __toESM(require("classnames"));
355
+ var import_react13 = __toESM(require("react"));
356
+ var import_react14 = require("react");
357
+
358
+ // src/drop-image/utils.ts
359
+ var wait = (d) => new Promise((r) => setTimeout(r, d));
360
+ var noop = () => {
361
+ };
362
+
363
+ // src/drop-image/useDropImage.ts
364
+ var import_react12 = require("react");
365
+ var useDropImage = (params) => {
366
+ const [isDragOver, setIsDragOver] = (0, import_react12.useState)(false);
367
+ const [imageSrc, setImageSrc] = (0, import_react12.useState)("");
368
+ const [imageSize, setImageSize] = (0, import_react12.useState)({ width: 0, height: 0 });
369
+ const imageFile = (0, import_react12.useRef)(null);
370
+ const {
371
+ portraitImageWidth = 384,
372
+ landscapeImageWidth = 500,
373
+ onDrop = () => {
374
+ },
375
+ onImageLoad = () => {
376
+ }
377
+ } = params || {};
378
+ const reset = () => {
379
+ setImageSrc("");
380
+ setImageSize({ width: 0, height: 0 });
381
+ setIsDragOver(false);
382
+ imageFile.current = null;
383
+ };
384
+ const calcDefaultImgSize = (img, defaultWidth) => {
385
+ const { width, height } = img;
386
+ const whRatio = width / height;
387
+ const isHorizontal = width > height;
388
+ const finalWidth = defaultWidth || (isHorizontal ? landscapeImageWidth : portraitImageWidth);
389
+ return {
390
+ width: finalWidth,
391
+ height: finalWidth / whRatio
392
+ };
393
+ };
394
+ const setPreviewImageSrcByFile = (file) => {
395
+ setImageSrc(URL.createObjectURL(file));
396
+ };
397
+ const handleDragOver = (0, import_react12.useCallback)((ev) => {
398
+ ev.preventDefault();
399
+ setIsDragOver(true);
400
+ ev.dataTransfer.dropEffect = "move";
401
+ }, []);
402
+ const handleDragLeave = (0, import_react12.useCallback)(() => {
403
+ setIsDragOver(false);
404
+ }, []);
405
+ const handleDrop = (0, import_react12.useCallback)((ev) => {
406
+ ev.preventDefault();
407
+ setIsDragOver(false);
408
+ const file = ev.dataTransfer.files[0];
409
+ imageFile.current = file;
410
+ setPreviewImageSrcByFile(file);
411
+ onDrop(ev, file);
412
+ }, []);
413
+ const handleImgLoad = (img) => {
414
+ const updateFunc = async () => {
415
+ const p = "decode" in img ? img.decode : Promise.resolve;
416
+ try {
417
+ await p();
418
+ } catch (e) {
419
+ }
420
+ setImageSize(calcDefaultImgSize({
421
+ width: img.naturalWidth,
422
+ height: img.naturalHeight
423
+ }));
424
+ if (!imageFile.current) {
425
+ return;
426
+ }
427
+ onImageLoad(img, imageFile.current);
428
+ };
429
+ if (img.complete) {
430
+ updateFunc().then();
431
+ return;
432
+ }
433
+ img.onload = updateFunc;
434
+ };
435
+ const imageRef = (0, import_react12.useCallback)((input) => {
436
+ if (!input) {
437
+ return;
438
+ }
439
+ handleImgLoad(input);
440
+ }, []);
441
+ return {
442
+ isDragOver,
443
+ imageSrc,
444
+ imageRef,
445
+ imageSize,
446
+ handleDragOver,
447
+ handleDragLeave,
448
+ handleDrop,
449
+ reset
450
+ };
451
+ };
452
+
453
+ // src/drop-image/DropImage.tsx
454
+ var DropImage = (props) => {
455
+ const {
456
+ uploader,
457
+ defaultBgColor = "#F3F4F6",
458
+ onDrop,
459
+ dragOverBgColor = "#EFF6FF",
460
+ waitTimeAfterFinish = 2e3,
461
+ placeholder = "",
462
+ onUploadFinish = noop,
463
+ dropAreaStyle = {
464
+ width: 300,
465
+ height: 300
466
+ }
467
+ } = props;
468
+ const [showImagePreviewer, setShowImagePreviewer] = (0, import_react14.useState)(false);
469
+ const {
470
+ handleDragLeave,
471
+ handleDragOver,
472
+ handleDrop,
473
+ imageRef,
474
+ imageSize,
475
+ imageSrc,
476
+ isDragOver,
477
+ reset
478
+ } = useDropImage({
479
+ onDrop,
480
+ onImageLoad: async (image, file) => {
481
+ await wait(500);
482
+ setShowImagePreviewer(true);
483
+ await onUploadFinish(await uploader(file, image));
484
+ await wait(waitTimeAfterFinish);
485
+ setShowImagePreviewer(false);
486
+ await wait(500);
487
+ reset();
488
+ }
489
+ });
490
+ const getDropAreaStyle = () => {
491
+ return Object.assign({}, dropAreaStyle, {
492
+ background: isDragOver ? dragOverBgColor : defaultBgColor,
493
+ width: imageSize.width || dropAreaStyle.width,
494
+ height: imageSize.height || dropAreaStyle.height
495
+ });
496
+ };
497
+ return /* @__PURE__ */ import_react13.default.createElement("div", {
498
+ className: (0, import_classnames6.default)("transition-all items-center justify-center flex duration-200 ease-in-out", {
499
+ "shadow-input": !imageSrc,
500
+ "shadow-empty": imageSrc
501
+ }),
502
+ onDragLeave: handleDragLeave,
503
+ onDragOver: handleDragOver,
504
+ onDrop: handleDrop,
505
+ style: getDropAreaStyle()
506
+ }, /* @__PURE__ */ import_react13.default.createElement("img", {
507
+ className: (0, import_classnames6.default)("max-w-[100%]", "h-[auto]", "duration-300", "transition-opacity", "opacity-0", {
508
+ "opacity-100": showImagePreviewer
509
+ }),
510
+ ref: imageRef,
511
+ src: imageSrc,
512
+ width: imageSize.width,
513
+ height: imageSize.height
514
+ }), !imageSrc && placeholder);
515
+ };
351
516
  module.exports = __toCommonJS(src_exports);
352
517
  // Annotate the CommonJS export names for ESM import in node:
353
518
  0 && (module.exports = {
@@ -355,6 +520,7 @@ module.exports = __toCommonJS(src_exports);
355
520
  Breadcrumb,
356
521
  Button,
357
522
  ButtonType,
523
+ DropImage,
358
524
  Link,
359
525
  LinkColor,
360
526
  Logo,
package/dist/index.mjs CHANGED
@@ -302,11 +302,176 @@ var Skeleton = (props) => {
302
302
  style: { width, height }
303
303
  });
304
304
  };
305
+
306
+ // src/drop-image/DropImage.tsx
307
+ import cls from "classnames";
308
+ import React13 from "react";
309
+ import { useState as useState3 } from "react";
310
+
311
+ // src/drop-image/utils.ts
312
+ var wait = (d) => new Promise((r) => setTimeout(r, d));
313
+ var noop = () => {
314
+ };
315
+
316
+ // src/drop-image/useDropImage.ts
317
+ import { useState as useState2, useCallback, useRef } from "react";
318
+ var useDropImage = (params) => {
319
+ const [isDragOver, setIsDragOver] = useState2(false);
320
+ const [imageSrc, setImageSrc] = useState2("");
321
+ const [imageSize, setImageSize] = useState2({ width: 0, height: 0 });
322
+ const imageFile = useRef(null);
323
+ const {
324
+ portraitImageWidth = 384,
325
+ landscapeImageWidth = 500,
326
+ onDrop = () => {
327
+ },
328
+ onImageLoad = () => {
329
+ }
330
+ } = params || {};
331
+ const reset = () => {
332
+ setImageSrc("");
333
+ setImageSize({ width: 0, height: 0 });
334
+ setIsDragOver(false);
335
+ imageFile.current = null;
336
+ };
337
+ const calcDefaultImgSize = (img, defaultWidth) => {
338
+ const { width, height } = img;
339
+ const whRatio = width / height;
340
+ const isHorizontal = width > height;
341
+ const finalWidth = defaultWidth || (isHorizontal ? landscapeImageWidth : portraitImageWidth);
342
+ return {
343
+ width: finalWidth,
344
+ height: finalWidth / whRatio
345
+ };
346
+ };
347
+ const setPreviewImageSrcByFile = (file) => {
348
+ setImageSrc(URL.createObjectURL(file));
349
+ };
350
+ const handleDragOver = useCallback((ev) => {
351
+ ev.preventDefault();
352
+ setIsDragOver(true);
353
+ ev.dataTransfer.dropEffect = "move";
354
+ }, []);
355
+ const handleDragLeave = useCallback(() => {
356
+ setIsDragOver(false);
357
+ }, []);
358
+ const handleDrop = useCallback((ev) => {
359
+ ev.preventDefault();
360
+ setIsDragOver(false);
361
+ const file = ev.dataTransfer.files[0];
362
+ imageFile.current = file;
363
+ setPreviewImageSrcByFile(file);
364
+ onDrop(ev, file);
365
+ }, []);
366
+ const handleImgLoad = (img) => {
367
+ const updateFunc = async () => {
368
+ const p = "decode" in img ? img.decode : Promise.resolve;
369
+ try {
370
+ await p();
371
+ } catch (e) {
372
+ }
373
+ setImageSize(calcDefaultImgSize({
374
+ width: img.naturalWidth,
375
+ height: img.naturalHeight
376
+ }));
377
+ if (!imageFile.current) {
378
+ return;
379
+ }
380
+ onImageLoad(img, imageFile.current);
381
+ };
382
+ if (img.complete) {
383
+ updateFunc().then();
384
+ return;
385
+ }
386
+ img.onload = updateFunc;
387
+ };
388
+ const imageRef = useCallback((input) => {
389
+ if (!input) {
390
+ return;
391
+ }
392
+ handleImgLoad(input);
393
+ }, []);
394
+ return {
395
+ isDragOver,
396
+ imageSrc,
397
+ imageRef,
398
+ imageSize,
399
+ handleDragOver,
400
+ handleDragLeave,
401
+ handleDrop,
402
+ reset
403
+ };
404
+ };
405
+
406
+ // src/drop-image/DropImage.tsx
407
+ var DropImage = (props) => {
408
+ const {
409
+ uploader,
410
+ defaultBgColor = "#F3F4F6",
411
+ onDrop,
412
+ dragOverBgColor = "#EFF6FF",
413
+ waitTimeAfterFinish = 2e3,
414
+ placeholder = "",
415
+ onUploadFinish = noop,
416
+ dropAreaStyle = {
417
+ width: 300,
418
+ height: 300
419
+ }
420
+ } = props;
421
+ const [showImagePreviewer, setShowImagePreviewer] = useState3(false);
422
+ const {
423
+ handleDragLeave,
424
+ handleDragOver,
425
+ handleDrop,
426
+ imageRef,
427
+ imageSize,
428
+ imageSrc,
429
+ isDragOver,
430
+ reset
431
+ } = useDropImage({
432
+ onDrop,
433
+ onImageLoad: async (image, file) => {
434
+ await wait(500);
435
+ setShowImagePreviewer(true);
436
+ await onUploadFinish(await uploader(file, image));
437
+ await wait(waitTimeAfterFinish);
438
+ setShowImagePreviewer(false);
439
+ await wait(500);
440
+ reset();
441
+ }
442
+ });
443
+ const getDropAreaStyle = () => {
444
+ return Object.assign({}, dropAreaStyle, {
445
+ background: isDragOver ? dragOverBgColor : defaultBgColor,
446
+ width: imageSize.width || dropAreaStyle.width,
447
+ height: imageSize.height || dropAreaStyle.height
448
+ });
449
+ };
450
+ return /* @__PURE__ */ React13.createElement("div", {
451
+ className: cls("transition-all items-center justify-center flex duration-200 ease-in-out", {
452
+ "shadow-input": !imageSrc,
453
+ "shadow-empty": imageSrc
454
+ }),
455
+ onDragLeave: handleDragLeave,
456
+ onDragOver: handleDragOver,
457
+ onDrop: handleDrop,
458
+ style: getDropAreaStyle()
459
+ }, /* @__PURE__ */ React13.createElement("img", {
460
+ className: cls("max-w-[100%]", "h-[auto]", "duration-300", "transition-opacity", "opacity-0", {
461
+ "opacity-100": showImagePreviewer
462
+ }),
463
+ ref: imageRef,
464
+ src: imageSrc,
465
+ width: imageSize.width,
466
+ height: imageSize.height
467
+ }), !imageSrc && placeholder);
468
+ };
305
469
  export {
306
470
  Article,
307
471
  Breadcrumb,
308
472
  Button,
309
473
  ButtonType,
474
+ DropImage,
310
475
  Link,
311
476
  LinkColor,
312
477
  Logo,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbki.ng/components",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",