@geoinsight/react-components 1.1.0 → 1.1.2
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/.yarn/install-state.gz +0 -0
- package/dist/cjs/components/menu/index.stories.d.ts +1 -0
- package/dist/cjs/components/range/hooks.d.ts +1 -1
- package/dist/cjs/components/range/index.d.ts +1 -1
- package/dist/cjs/components/range/index.stories.d.ts +1 -0
- package/dist/cjs/components/range/index.types.d.ts +4 -0
- package/dist/cjs/index.css +120 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +237 -46
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/components/menu/index.stories.d.ts +1 -0
- package/dist/esm/components/range/hooks.d.ts +1 -1
- package/dist/esm/components/range/index.d.ts +1 -1
- package/dist/esm/components/range/index.stories.d.ts +1 -0
- package/dist/esm/components/range/index.types.d.ts +4 -0
- package/dist/esm/index.css +120 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +238 -48
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -5,7 +5,8 @@ import { TfiAngleDown, TfiAngleUp } from 'react-icons/tfi';
|
|
|
5
5
|
import { BsCheckCircleFill, BsXCircleFill } from 'react-icons/bs';
|
|
6
6
|
import { useForm, FormProvider, useFormContext, Controller } from 'react-hook-form';
|
|
7
7
|
import { TbArrowsDiagonal2 } from 'react-icons/tb';
|
|
8
|
-
import { IoClose } from 'react-icons/io5';
|
|
8
|
+
import { IoClose, IoPause, IoPlay, IoStop } from 'react-icons/io5';
|
|
9
|
+
import { RiMapPinTimeFill, RiMapPinTimeLine } from 'react-icons/ri';
|
|
9
10
|
|
|
10
11
|
const generateId = () => {
|
|
11
12
|
return `id-${Math.random().toString(36).slice(2, 11)}`;
|
|
@@ -290,6 +291,241 @@ function Modal({ className, modalref, children, title, subtitle, footer, hasOver
|
|
|
290
291
|
return (jsxs(Fragment, { children: [hasOverlay && jsx("div", { className: "modal-overlay" }), jsxs("div", { ref: modalref, className: clsx("modal", className), ...rest, children: [hasCloseButton && (jsx(Button, { mode: "secondary", className: "modal__close", size: "small", onClick: handleClose, children: jsx(IoClose, {}) })), jsxs("div", { className: "modal__content", children: [title && (jsxs("div", { className: "modal__header", children: [jsx("h3", { children: title }), jsx("h6", { children: subtitle })] })), jsx("div", { className: "modal__children", children: children }), footer && jsx("div", { className: "modal__footer", children: footer })] })] })] }));
|
|
291
292
|
}
|
|
292
293
|
|
|
294
|
+
function usePositions(panelRef, initial) {
|
|
295
|
+
const [allPositions, setAllPositions] = useState(undefined);
|
|
296
|
+
const [pixelPosition, setPixelPosition] = useState(undefined);
|
|
297
|
+
const [thumbPosition, setThumbPosition] = useState(undefined);
|
|
298
|
+
// find positions of all values
|
|
299
|
+
useEffect(() => {
|
|
300
|
+
if (panelRef?.current) {
|
|
301
|
+
const current = panelRef.current;
|
|
302
|
+
if (current) {
|
|
303
|
+
const valuesRef = Array.from(current.getElementsByClassName("range__content"));
|
|
304
|
+
const parentBounds = current.getBoundingClientRect();
|
|
305
|
+
setAllPositions(valuesRef.map((values) => {
|
|
306
|
+
const childBounds = values.getBoundingClientRect();
|
|
307
|
+
return childBounds.right - parentBounds.x;
|
|
308
|
+
}));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}, []);
|
|
312
|
+
// initial position
|
|
313
|
+
useEffect(() => {
|
|
314
|
+
if (allPositions) {
|
|
315
|
+
setPixelPosition(`${allPositions[initial]}px`);
|
|
316
|
+
setThumbPosition(`${allPositions[initial] -
|
|
317
|
+
(allPositions[0] / 2 + getThumbBounds().width / 2)}px`);
|
|
318
|
+
}
|
|
319
|
+
}, [allPositions]);
|
|
320
|
+
return {
|
|
321
|
+
panelRef,
|
|
322
|
+
allPositions,
|
|
323
|
+
setAllPositions,
|
|
324
|
+
pixelPosition,
|
|
325
|
+
setPixelPosition,
|
|
326
|
+
thumbPosition,
|
|
327
|
+
setThumbPosition,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function getThumbBounds() {
|
|
331
|
+
const thumb = document.getElementsByClassName("range__thumb")[0];
|
|
332
|
+
return thumb.getBoundingClientRect();
|
|
333
|
+
}
|
|
334
|
+
function useTimer({ allPositions, indexPosition, handlePosition, handleOverContent, values, }) {
|
|
335
|
+
const timeoutRef = useRef(null);
|
|
336
|
+
const [isPlay, setIsPlay] = useState(false);
|
|
337
|
+
// timer
|
|
338
|
+
useEffect(() => {
|
|
339
|
+
resetTimeout();
|
|
340
|
+
if (isPlay) {
|
|
341
|
+
if (allPositions) {
|
|
342
|
+
timeoutRef.current = setTimeout(() => {
|
|
343
|
+
const newThumbPosition = allPositions[indexPosition] +
|
|
344
|
+
allPositions[0] / 2 -
|
|
345
|
+
getThumbBounds().width / 2;
|
|
346
|
+
handlePosition(allPositions[indexPosition + 1], newThumbPosition, indexPosition + 1, true);
|
|
347
|
+
handleOverContent(values[indexPosition + 1]);
|
|
348
|
+
}, 500);
|
|
349
|
+
if (indexPosition === allPositions.length - 1) {
|
|
350
|
+
setIsPlay(false);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return () => {
|
|
355
|
+
resetTimeout();
|
|
356
|
+
};
|
|
357
|
+
}, [isPlay, indexPosition]);
|
|
358
|
+
const resetTimeout = () => {
|
|
359
|
+
if (timeoutRef.current) {
|
|
360
|
+
clearTimeout(timeoutRef.current);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
const handlePlay = () => {
|
|
364
|
+
setIsPlay(!isPlay);
|
|
365
|
+
};
|
|
366
|
+
const handleStop = () => {
|
|
367
|
+
if (allPositions) {
|
|
368
|
+
handlePosition(allPositions[0], allPositions[0] / 2 - getThumbBounds().width / 2, 0, false);
|
|
369
|
+
setIsPlay(false);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
return { isPlay, setIsPlay, handlePlay, handleStop };
|
|
373
|
+
}
|
|
374
|
+
function useThumb({ allPositions, panelRef, handlePosition, handleOverContent, values, }) {
|
|
375
|
+
// Add drag
|
|
376
|
+
const startDragging = (event) => {
|
|
377
|
+
if (allPositions && panelRef?.current) {
|
|
378
|
+
const current = panelRef.current;
|
|
379
|
+
const panelBounds = current?.getBoundingClientRect();
|
|
380
|
+
const x = event.pageX - panelBounds.left;
|
|
381
|
+
const absX = Math.abs(x);
|
|
382
|
+
if (x >= 0 && x <= allPositions[allPositions.length - 1]) {
|
|
383
|
+
const desiredPosition = allPositions.reduce((prev, curr) => Math.abs(prev - absX) < Math.abs(curr - absX) ? prev : curr);
|
|
384
|
+
const index = allPositions.indexOf(desiredPosition);
|
|
385
|
+
const newThumbPosition = index === 0
|
|
386
|
+
? allPositions[index] -
|
|
387
|
+
allPositions[0] / 2 -
|
|
388
|
+
getThumbBounds().width / 2
|
|
389
|
+
: allPositions[index - 1] +
|
|
390
|
+
allPositions[0] / 2 -
|
|
391
|
+
getThumbBounds().width / 2;
|
|
392
|
+
handlePosition(desiredPosition, newThumbPosition, index, true);
|
|
393
|
+
handleOverContent(values[index]);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const stopDragging = () => {
|
|
398
|
+
window.removeEventListener("mousemove", startDragging, false);
|
|
399
|
+
window.removeEventListener("mouseup", stopDragging, false);
|
|
400
|
+
};
|
|
401
|
+
const handleDownThumb = () => {
|
|
402
|
+
window.addEventListener("mousemove", startDragging, false);
|
|
403
|
+
window.addEventListener("mouseup", stopDragging, false);
|
|
404
|
+
};
|
|
405
|
+
return { handleDownThumb };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const ThemeContext = createContext(undefined);
|
|
409
|
+
function ThemeProvider({ children, dataTheme: dataThemeProp, dataPalette: dataPaletteProp = "water", }) {
|
|
410
|
+
const [dataTheme, setDataTheme] = useState();
|
|
411
|
+
const [dataPalette, setDataPalette] = useState();
|
|
412
|
+
useEffect(() => {
|
|
413
|
+
if (dataPaletteProp) {
|
|
414
|
+
switchDataPalette(dataPaletteProp);
|
|
415
|
+
}
|
|
416
|
+
}, [dataPaletteProp]);
|
|
417
|
+
useEffect(() => {
|
|
418
|
+
if (dataThemeProp) {
|
|
419
|
+
switchDataTheme(dataThemeProp);
|
|
420
|
+
}
|
|
421
|
+
}, [dataThemeProp]);
|
|
422
|
+
useEffect(() => {
|
|
423
|
+
if (window.matchMedia) {
|
|
424
|
+
if (localStorage.getItem("data-theme")) {
|
|
425
|
+
document.documentElement.setAttribute("data-theme", localStorage.getItem("data-theme"));
|
|
426
|
+
setDataTheme(localStorage.getItem("data-theme"));
|
|
427
|
+
}
|
|
428
|
+
else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
429
|
+
document.documentElement.setAttribute("data-theme", "dark");
|
|
430
|
+
setDataTheme("dark");
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}, []);
|
|
434
|
+
const switchDataTheme = (mode) => {
|
|
435
|
+
document.documentElement.setAttribute("data-theme", mode);
|
|
436
|
+
setDataTheme(mode);
|
|
437
|
+
localStorage.setItem("data-theme", mode);
|
|
438
|
+
};
|
|
439
|
+
const switchDataPalette = (mode) => {
|
|
440
|
+
document.documentElement.setAttribute("data-palette", mode);
|
|
441
|
+
setDataPalette(mode);
|
|
442
|
+
localStorage.setItem("palette-theme", mode);
|
|
443
|
+
};
|
|
444
|
+
return (jsx(ThemeContext.Provider, { value: { dataTheme, switchDataTheme, dataPalette, switchDataPalette }, children: children }));
|
|
445
|
+
}
|
|
446
|
+
function useTheme() {
|
|
447
|
+
const context = useContext(ThemeContext);
|
|
448
|
+
if (context === undefined) {
|
|
449
|
+
throw new Error("useTheme must be used within a ThemeProvider");
|
|
450
|
+
}
|
|
451
|
+
return context;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function RangeThumb({ thumbPosition, handleOverContent, handleDownThumb }) {
|
|
455
|
+
const { dataTheme } = useTheme();
|
|
456
|
+
return (jsx(Button, { mode: "icon", className: "range__thumb", size: "small", style: {
|
|
457
|
+
left: `${thumbPosition}`,
|
|
458
|
+
}, onMouseOver: () => handleOverContent(thumbPosition), onMouseDown: handleDownThumb, children: dataTheme === "dark" ? (jsx(RiMapPinTimeFill, { size: "1.5rem" })) : (jsx(RiMapPinTimeLine, { size: "1.5rem" })) }));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function RangeContent({ period, values, allPositions, handlePosition, handleOverContent, handleOutContent, over, }) {
|
|
462
|
+
const handleClickRange = (index) => {
|
|
463
|
+
if (allPositions) {
|
|
464
|
+
const newThumbPosition = index === 0
|
|
465
|
+
? allPositions[index] -
|
|
466
|
+
allPositions[0] / 2 -
|
|
467
|
+
getThumbBounds().width / 2
|
|
468
|
+
: allPositions[index - 1] +
|
|
469
|
+
allPositions[0] / 2 -
|
|
470
|
+
getThumbBounds().width / 2;
|
|
471
|
+
handlePosition(allPositions[index], newThumbPosition, index, true);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
return period
|
|
475
|
+
? values.map((value, index) => (jsx("div", { className: "range__content range__content__period", onClick: () => handleClickRange(index), onMouseOver: () => handleOverContent(value), onMouseOut: () => handleOutContent(), children: index % period === 0 ? (jsxs(Fragment, { children: [jsx("div", { className: "range__tick range__tick__large" }), jsx("div", { className: clsx("range__value", "range__value__period", {
|
|
476
|
+
"range__value--over": value === over,
|
|
477
|
+
}), children: value })] })) : (jsxs(Fragment, { children: [jsx("div", { className: "range__tick range__tick__small" }), value === over && (jsx("div", { className: clsx("range__value", "range__value__period", {
|
|
478
|
+
"range__value--over": value === over,
|
|
479
|
+
}), children: value }))] })) })))
|
|
480
|
+
: values.map((value, index) => (jsxs("div", { className: "range__content", onClick: () => handleClickRange(index), children: [jsx("div", { className: "range__tick range__tick__large" }), jsx("div", { className: "range__value", children: value })] })));
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function RangeControls({ indexPosition, isPlay, handlePlay, handleStop, }) {
|
|
484
|
+
return (jsxs(Fragment, { children: [jsx(Button, { mode: "icon", size: "small", className: "range__play", onClick: handlePlay, children: isPlay ? (jsx(IoPause, { color: "var(--color-primary-base)", size: "2rem" })) : (jsx(IoPlay, { color: "var(--color-primary-base)", size: "2rem" })) }), jsx(Button, { mode: "icon", size: "small", className: "range__play", disabled: !isPlay && indexPosition === 0, onClick: handleStop, children: jsx(IoStop, { color: "var(--color-primary-base)", size: "2rem" }) })] }));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function Range({ initial = 0, period, values, handleSelected }) {
|
|
488
|
+
const panelRef = useRef(null);
|
|
489
|
+
const [indexPosition, setIndexPosition] = useState(initial);
|
|
490
|
+
const [over, setOver] = useState(undefined);
|
|
491
|
+
const { allPositions, pixelPosition, setPixelPosition, thumbPosition, setThumbPosition, } = usePositions(panelRef, initial);
|
|
492
|
+
const handlePosition = (pixelPosition, thumbPosition, index, isHandleSelected = false) => {
|
|
493
|
+
setPixelPosition(`${pixelPosition}px`);
|
|
494
|
+
setThumbPosition(`${thumbPosition}px`);
|
|
495
|
+
setIndexPosition(index);
|
|
496
|
+
if (isHandleSelected && handleSelected) {
|
|
497
|
+
handleSelected({
|
|
498
|
+
value: values[index],
|
|
499
|
+
index,
|
|
500
|
+
position: pixelPosition,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
const handleOverContent = (value) => {
|
|
505
|
+
setOver(value);
|
|
506
|
+
};
|
|
507
|
+
const handleOutContent = () => {
|
|
508
|
+
setOver(undefined);
|
|
509
|
+
};
|
|
510
|
+
const { isPlay, handlePlay, handleStop } = useTimer({
|
|
511
|
+
allPositions,
|
|
512
|
+
indexPosition,
|
|
513
|
+
handlePosition,
|
|
514
|
+
handleOverContent,
|
|
515
|
+
values,
|
|
516
|
+
});
|
|
517
|
+
const { handleDownThumb } = useThumb({
|
|
518
|
+
allPositions,
|
|
519
|
+
panelRef,
|
|
520
|
+
handlePosition,
|
|
521
|
+
handleOverContent,
|
|
522
|
+
values,
|
|
523
|
+
});
|
|
524
|
+
return (jsxs("div", { className: "range", children: [jsxs("div", { ref: panelRef, className: "range__panel", children: [jsx(RangeContent, { period: period, values: values, allPositions: allPositions, handlePosition: handlePosition, handleOverContent: handleOverContent, handleOutContent: handleOutContent, over: over }), jsx("div", { className: "range__track", style: {
|
|
525
|
+
width: `${pixelPosition}`,
|
|
526
|
+
} }), jsx(RangeThumb, { thumbPosition: thumbPosition, handleOverContent: handleOverContent, handleDownThumb: handleDownThumb })] }), jsx(RangeControls, { indexPosition: indexPosition, isPlay: isPlay, handlePlay: handlePlay, handleStop: handleStop })] }));
|
|
527
|
+
}
|
|
528
|
+
|
|
293
529
|
const LoadingContext = createContext(undefined);
|
|
294
530
|
function loadingReducer(state, action) {
|
|
295
531
|
switch (action.type) {
|
|
@@ -421,51 +657,5 @@ function useModal({} = {}) {
|
|
|
421
657
|
return context;
|
|
422
658
|
}
|
|
423
659
|
|
|
424
|
-
|
|
425
|
-
function ThemeProvider({ children, dataTheme: dataThemeProp, dataPalette: dataPaletteProp = "water", }) {
|
|
426
|
-
const [dataTheme, setDataTheme] = useState();
|
|
427
|
-
const [dataPalette, setDataPalette] = useState();
|
|
428
|
-
useEffect(() => {
|
|
429
|
-
if (dataPaletteProp) {
|
|
430
|
-
switchDataPalette(dataPaletteProp);
|
|
431
|
-
}
|
|
432
|
-
}, [dataPaletteProp]);
|
|
433
|
-
useEffect(() => {
|
|
434
|
-
if (dataThemeProp) {
|
|
435
|
-
switchDataTheme(dataThemeProp);
|
|
436
|
-
}
|
|
437
|
-
}, [dataThemeProp]);
|
|
438
|
-
useEffect(() => {
|
|
439
|
-
if (window.matchMedia) {
|
|
440
|
-
if (localStorage.getItem("data-theme")) {
|
|
441
|
-
document.documentElement.setAttribute("data-theme", localStorage.getItem("data-theme"));
|
|
442
|
-
setDataTheme(localStorage.getItem("data-theme"));
|
|
443
|
-
}
|
|
444
|
-
else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
445
|
-
document.documentElement.setAttribute("data-theme", "dark");
|
|
446
|
-
setDataTheme("dark");
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
}, []);
|
|
450
|
-
const switchDataTheme = (mode) => {
|
|
451
|
-
document.documentElement.setAttribute("data-theme", mode);
|
|
452
|
-
setDataTheme(mode);
|
|
453
|
-
localStorage.setItem("data-theme", mode);
|
|
454
|
-
};
|
|
455
|
-
const switchDataPalette = (mode) => {
|
|
456
|
-
document.documentElement.setAttribute("data-palette", mode);
|
|
457
|
-
setDataPalette(mode);
|
|
458
|
-
localStorage.setItem("palette-theme", mode);
|
|
459
|
-
};
|
|
460
|
-
return (jsx(ThemeContext.Provider, { value: { dataTheme, switchDataTheme, dataPalette, switchDataPalette }, children: children }));
|
|
461
|
-
}
|
|
462
|
-
function useTheme() {
|
|
463
|
-
const context = useContext(ThemeContext);
|
|
464
|
-
if (context === undefined) {
|
|
465
|
-
throw new Error("useTheme must be used within a ThemeProvider");
|
|
466
|
-
}
|
|
467
|
-
return context;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
export { Button, Form, FormInput, FormSelect, FormTextArea, Input, Loading, LoadingContext, LoadingProvider, Menu, Modal, ModalContext, ModalProvider, Select, TextArea, ThemeContext, ThemeProvider, useLoading, useModal, useTheme };
|
|
660
|
+
export { Button, Form, FormInput, FormSelect, FormTextArea, Input, Loading, LoadingContext, LoadingProvider, Menu, Modal, ModalContext, ModalProvider, Range, Select, TextArea, ThemeContext, ThemeProvider, useLoading, useModal, useTheme };
|
|
471
661
|
//# sourceMappingURL=index.js.map
|