jy-headless 0.3.0 → 0.3.7

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.
Files changed (52) hide show
  1. package/Input/NumberInput.d.ts +3 -0
  2. package/Input/NumberInput.type.d.ts +31 -0
  3. package/Input/TextInput.d.ts +3 -0
  4. package/Input/TextInput.type.d.ts +95 -0
  5. package/Input/index.d.ts +2 -0
  6. package/Popover/Popover.d.ts +2 -2
  7. package/Popover/Popover.js +15 -62
  8. package/Popover/Popover.type.d.ts +6 -3
  9. package/Select/Select.d.ts +51 -0
  10. package/Select/Select.js +176 -0
  11. package/Select/Select.type.d.ts +52 -0
  12. package/Select/index.d.ts +1 -0
  13. package/Tooltip/Tooltip.d.ts +10 -0
  14. package/Tooltip/Tooltip.js +36 -0
  15. package/Tooltip/Tooltip.type.d.ts +20 -0
  16. package/Tooltip/index.d.ts +2 -0
  17. package/cjs/Input/NumberInput.d.ts +3 -0
  18. package/cjs/Input/NumberInput.type.d.ts +31 -0
  19. package/cjs/Input/TextInput.d.ts +3 -0
  20. package/cjs/Input/TextInput.type.d.ts +95 -0
  21. package/cjs/Input/index.d.ts +2 -0
  22. package/cjs/Popover/Popover.d.ts +2 -2
  23. package/cjs/Popover/Popover.js +13 -60
  24. package/cjs/Popover/Popover.type.d.ts +6 -3
  25. package/cjs/Select/Select.d.ts +51 -0
  26. package/cjs/Select/Select.js +178 -0
  27. package/cjs/Select/Select.type.d.ts +52 -0
  28. package/cjs/Select/index.d.ts +1 -0
  29. package/cjs/Tooltip/Tooltip.d.ts +10 -0
  30. package/cjs/Tooltip/Tooltip.js +38 -0
  31. package/cjs/Tooltip/Tooltip.type.d.ts +20 -0
  32. package/cjs/Tooltip/index.d.ts +2 -0
  33. package/cjs/hooks/index.d.ts +3 -0
  34. package/cjs/hooks/useDebounce.d.ts +1 -0
  35. package/cjs/hooks/useDebounce.js +24 -0
  36. package/cjs/hooks/usePortal.d.ts +23 -0
  37. package/cjs/hooks/usePortal.js +80 -0
  38. package/cjs/hooks/useThrottle.d.ts +1 -0
  39. package/cjs/hooks/useThrottle.js +34 -0
  40. package/cjs/index.d.ts +4 -1
  41. package/cjs/index.js +11 -2
  42. package/hooks/index.d.ts +3 -0
  43. package/hooks/useDebounce.d.ts +1 -0
  44. package/hooks/useDebounce.js +22 -0
  45. package/hooks/usePortal.d.ts +23 -0
  46. package/hooks/usePortal.js +78 -0
  47. package/hooks/useThrottle.d.ts +1 -0
  48. package/hooks/useThrottle.js +32 -0
  49. package/index.d.ts +4 -1
  50. package/index.js +7 -1
  51. package/package.json +31 -6
  52. package/version.txt +1 -1
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ var jsxRuntime = require('react/jsx-runtime');
4
+ var react = require('react');
5
+ var reactDom = require('react-dom');
6
+
7
+ const usePortal = ({ content, key, visible = true, targetRef, popoverRef, direction = 'top', gap = 0, position: customPosition, autoFlip = true, }) => {
8
+ const rootDom = react.useMemo(() => document.body, []);
9
+ const [position, setPosition] = react.useState(customPosition || { top: 0, left: 0 });
10
+ const [finalDirection, setFinalDirection] = react.useState(direction);
11
+ const getOppositeDirection = (dir) => {
12
+ if (dir.startsWith('top'))
13
+ return dir.replace('top', 'bottom');
14
+ if (dir.startsWith('bottom'))
15
+ return dir.replace('bottom', 'top');
16
+ if (dir === 'left')
17
+ return 'right';
18
+ if (dir === 'right')
19
+ return 'left';
20
+ return dir;
21
+ };
22
+ const getPopoverPosition = (targetRect, popoverRect, dir) => {
23
+ let top = (dir.startsWith('top')
24
+ ? targetRect.top - popoverRect.height
25
+ : dir.startsWith('bottom')
26
+ ? targetRect.top + targetRect.height
27
+ : targetRect.top + targetRect.height / 2 - popoverRect.height / 2) - gap;
28
+ let left = dir.endsWith('left')
29
+ ? targetRect.left - popoverRect.width - gap
30
+ : (dir.endsWith('right')
31
+ ? targetRect.left + targetRect.width + gap
32
+ : targetRect.left + targetRect.width / 2 - popoverRect.width / 2);
33
+ return { top, left };
34
+ };
35
+ const isOutOfViewport = (pos, popoverRect) => {
36
+ const viewportWidth = window.innerWidth;
37
+ const viewportHeight = window.innerHeight;
38
+ return (pos.top < 0 || // 상단 밖
39
+ pos.left < 0 || // 좌측 밖
40
+ pos.top + popoverRect.height > viewportHeight + window.scrollY || // 하단 밖
41
+ pos.left + popoverRect.width > viewportWidth + window.scrollX // 우측 밖
42
+ );
43
+ };
44
+ react.useLayoutEffect(() => {
45
+ if (visible && targetRef?.current && popoverRef?.current) {
46
+ const targetRect = targetRef.current.getBoundingClientRect();
47
+ const popoverRect = popoverRef.current.getBoundingClientRect();
48
+ // 원래 방향으로 위치 계산
49
+ let calculatedPosition = getPopoverPosition(targetRect, popoverRect, direction);
50
+ let currentDirection = direction;
51
+ // autoFlip이 활성화되어 있고 화면 밖으로 나가면 반대 방향 시도
52
+ if (autoFlip && isOutOfViewport(calculatedPosition, popoverRect)) {
53
+ const oppositeDir = getOppositeDirection(direction);
54
+ const oppositePosition = getPopoverPosition(targetRect, popoverRect, oppositeDir);
55
+ // 반대 방향이 더 나으면 반대 방향 사용
56
+ if (!isOutOfViewport(oppositePosition, popoverRect)) {
57
+ calculatedPosition = oppositePosition;
58
+ currentDirection = oppositeDir;
59
+ }
60
+ }
61
+ setFinalDirection(currentDirection);
62
+ setPosition({
63
+ top: calculatedPosition.top + window.scrollY,
64
+ left: calculatedPosition.left + window.scrollX,
65
+ });
66
+ }
67
+ else if (customPosition) {
68
+ setPosition(customPosition);
69
+ }
70
+ }, [visible, direction, gap, customPosition, autoFlip]);
71
+ const wrappedContent = targetRef ? (jsxRuntime.jsx("span", { ref: popoverRef, style: {
72
+ position: 'absolute',
73
+ top: `${position.top}px`,
74
+ left: `${position.left}px`,
75
+ }, children: content })) : content;
76
+ const portal = reactDom.createPortal(wrappedContent, rootDom, key);
77
+ return { portal, rootDom, finalDirection };
78
+ };
79
+
80
+ module.exports = usePortal;
@@ -0,0 +1 @@
1
+ export declare const useThrottle: (callback: (...args: any[]) => void, delay: number) => (...args: any[]) => void;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ const useThrottle = (callback, delay) => {
6
+ const lastRun = react.useRef(Date.now());
7
+ const timeoutRef = react.useRef(0);
8
+ react.useEffect(() => {
9
+ return () => {
10
+ if (timeoutRef.current) {
11
+ clearTimeout(timeoutRef.current);
12
+ }
13
+ };
14
+ }, []);
15
+ return react.useCallback((...args) => {
16
+ const now = Date.now();
17
+ const timeSinceLastRun = now - lastRun.current;
18
+ if (timeSinceLastRun >= delay) {
19
+ callback(...args);
20
+ lastRun.current = now;
21
+ }
22
+ else {
23
+ if (timeoutRef.current) {
24
+ clearTimeout(timeoutRef.current);
25
+ }
26
+ timeoutRef.current = setTimeout(() => {
27
+ callback(...args);
28
+ lastRun.current = Date.now();
29
+ }, delay - timeSinceLastRun);
30
+ }
31
+ }, [callback, delay]);
32
+ };
33
+
34
+ exports.useThrottle = useThrottle;
package/cjs/index.d.ts CHANGED
@@ -1 +1,4 @@
1
- export * from './Popover';
1
+ export * from './hooks';
2
+ export * from './Tooltip';
3
+ export * from './Input';
4
+ export * from './Select';
package/cjs/index.js CHANGED
@@ -1,7 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var Popover = require('./Popover/Popover.js');
3
+ require('react/jsx-runtime');
4
+ require('react');
5
+ require('react-dom');
6
+ var useDebounce = require('./hooks/useDebounce.js');
7
+ var useThrottle = require('./hooks/useThrottle.js');
8
+ var Tooltip = require('./Tooltip/Tooltip.js');
9
+ var Select = require('./Select/Select.js');
4
10
 
5
11
 
6
12
 
7
- exports.Popover = Popover.Popover;
13
+ exports.useDebounce = useDebounce.useDebounce;
14
+ exports.useThrottle = useThrottle.useThrottle;
15
+ exports.Tooltip = Tooltip.Tooltip;
16
+ exports.useSelectContext = Select.useSelectContext;
@@ -0,0 +1,3 @@
1
+ export * from './usePortal';
2
+ export * from './useDebounce';
3
+ export * from './useThrottle';
@@ -0,0 +1 @@
1
+ export declare const useDebounce: (callback: (...args: any[]) => void, delay: number) => (...args: any[]) => void;
@@ -0,0 +1,22 @@
1
+ import { useRef, useEffect, useCallback } from 'react';
2
+
3
+ const useDebounce = (callback, delay) => {
4
+ const timeoutRef = useRef(0);
5
+ useEffect(() => {
6
+ return () => {
7
+ if (timeoutRef.current) {
8
+ clearTimeout(timeoutRef.current);
9
+ }
10
+ };
11
+ }, []);
12
+ return useCallback((...args) => {
13
+ if (timeoutRef.current) {
14
+ clearTimeout(timeoutRef.current);
15
+ }
16
+ timeoutRef.current = setTimeout(() => {
17
+ callback(...args);
18
+ }, delay);
19
+ }, [callback, delay]);
20
+ };
21
+
22
+ export { useDebounce };
@@ -0,0 +1,23 @@
1
+ import { Key, ReactNode, RefObject } from 'react';
2
+ type Position = {
3
+ top: number;
4
+ left: number;
5
+ };
6
+ export type Direction = 'top-left' | 'top-center' | 'top' | 'top-right' | 'left' | 'right' | 'bottom-left' | 'bottom' | 'bottom-center' | 'bottom-right';
7
+ type UsePortalProps = {
8
+ content: ReactNode;
9
+ key?: Key | null;
10
+ visible?: boolean;
11
+ targetRef?: RefObject<HTMLElement | null>;
12
+ popoverRef?: RefObject<HTMLElement | null>;
13
+ direction?: Direction;
14
+ gap?: number;
15
+ position?: Position;
16
+ autoFlip?: boolean;
17
+ };
18
+ declare const usePortal: ({ content, key, visible, targetRef, popoverRef, direction, gap, position: customPosition, autoFlip, }: UsePortalProps) => {
19
+ portal: import("react").ReactPortal;
20
+ rootDom: HTMLElement;
21
+ finalDirection: Direction;
22
+ };
23
+ export default usePortal;
@@ -0,0 +1,78 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import { useMemo, useState, useLayoutEffect } from 'react';
3
+ import { createPortal } from 'react-dom';
4
+
5
+ const usePortal = ({ content, key, visible = true, targetRef, popoverRef, direction = 'top', gap = 0, position: customPosition, autoFlip = true, }) => {
6
+ const rootDom = useMemo(() => document.body, []);
7
+ const [position, setPosition] = useState(customPosition || { top: 0, left: 0 });
8
+ const [finalDirection, setFinalDirection] = useState(direction);
9
+ const getOppositeDirection = (dir) => {
10
+ if (dir.startsWith('top'))
11
+ return dir.replace('top', 'bottom');
12
+ if (dir.startsWith('bottom'))
13
+ return dir.replace('bottom', 'top');
14
+ if (dir === 'left')
15
+ return 'right';
16
+ if (dir === 'right')
17
+ return 'left';
18
+ return dir;
19
+ };
20
+ const getPopoverPosition = (targetRect, popoverRect, dir) => {
21
+ let top = (dir.startsWith('top')
22
+ ? targetRect.top - popoverRect.height
23
+ : dir.startsWith('bottom')
24
+ ? targetRect.top + targetRect.height
25
+ : targetRect.top + targetRect.height / 2 - popoverRect.height / 2) - gap;
26
+ let left = dir.endsWith('left')
27
+ ? targetRect.left - popoverRect.width - gap
28
+ : (dir.endsWith('right')
29
+ ? targetRect.left + targetRect.width + gap
30
+ : targetRect.left + targetRect.width / 2 - popoverRect.width / 2);
31
+ return { top, left };
32
+ };
33
+ const isOutOfViewport = (pos, popoverRect) => {
34
+ const viewportWidth = window.innerWidth;
35
+ const viewportHeight = window.innerHeight;
36
+ return (pos.top < 0 || // 상단 밖
37
+ pos.left < 0 || // 좌측 밖
38
+ pos.top + popoverRect.height > viewportHeight + window.scrollY || // 하단 밖
39
+ pos.left + popoverRect.width > viewportWidth + window.scrollX // 우측 밖
40
+ );
41
+ };
42
+ useLayoutEffect(() => {
43
+ if (visible && targetRef?.current && popoverRef?.current) {
44
+ const targetRect = targetRef.current.getBoundingClientRect();
45
+ const popoverRect = popoverRef.current.getBoundingClientRect();
46
+ // 원래 방향으로 위치 계산
47
+ let calculatedPosition = getPopoverPosition(targetRect, popoverRect, direction);
48
+ let currentDirection = direction;
49
+ // autoFlip이 활성화되어 있고 화면 밖으로 나가면 반대 방향 시도
50
+ if (autoFlip && isOutOfViewport(calculatedPosition, popoverRect)) {
51
+ const oppositeDir = getOppositeDirection(direction);
52
+ const oppositePosition = getPopoverPosition(targetRect, popoverRect, oppositeDir);
53
+ // 반대 방향이 더 나으면 반대 방향 사용
54
+ if (!isOutOfViewport(oppositePosition, popoverRect)) {
55
+ calculatedPosition = oppositePosition;
56
+ currentDirection = oppositeDir;
57
+ }
58
+ }
59
+ setFinalDirection(currentDirection);
60
+ setPosition({
61
+ top: calculatedPosition.top + window.scrollY,
62
+ left: calculatedPosition.left + window.scrollX,
63
+ });
64
+ }
65
+ else if (customPosition) {
66
+ setPosition(customPosition);
67
+ }
68
+ }, [visible, direction, gap, customPosition, autoFlip]);
69
+ const wrappedContent = targetRef ? (jsx("span", { ref: popoverRef, style: {
70
+ position: 'absolute',
71
+ top: `${position.top}px`,
72
+ left: `${position.left}px`,
73
+ }, children: content })) : content;
74
+ const portal = createPortal(wrappedContent, rootDom, key);
75
+ return { portal, rootDom, finalDirection };
76
+ };
77
+
78
+ export { usePortal as default };
@@ -0,0 +1 @@
1
+ export declare const useThrottle: (callback: (...args: any[]) => void, delay: number) => (...args: any[]) => void;
@@ -0,0 +1,32 @@
1
+ import { useRef, useEffect, useCallback } from 'react';
2
+
3
+ const useThrottle = (callback, delay) => {
4
+ const lastRun = useRef(Date.now());
5
+ const timeoutRef = useRef(0);
6
+ useEffect(() => {
7
+ return () => {
8
+ if (timeoutRef.current) {
9
+ clearTimeout(timeoutRef.current);
10
+ }
11
+ };
12
+ }, []);
13
+ return useCallback((...args) => {
14
+ const now = Date.now();
15
+ const timeSinceLastRun = now - lastRun.current;
16
+ if (timeSinceLastRun >= delay) {
17
+ callback(...args);
18
+ lastRun.current = now;
19
+ }
20
+ else {
21
+ if (timeoutRef.current) {
22
+ clearTimeout(timeoutRef.current);
23
+ }
24
+ timeoutRef.current = setTimeout(() => {
25
+ callback(...args);
26
+ lastRun.current = Date.now();
27
+ }, delay - timeSinceLastRun);
28
+ }
29
+ }, [callback, delay]);
30
+ };
31
+
32
+ export { useThrottle };
package/index.d.ts CHANGED
@@ -1 +1,4 @@
1
- export * from './Popover';
1
+ export * from './hooks';
2
+ export * from './Tooltip';
3
+ export * from './Input';
4
+ export * from './Select';
package/index.js CHANGED
@@ -1 +1,7 @@
1
- export { Popover } from './Popover/Popover.js';
1
+ import 'react/jsx-runtime';
2
+ import 'react';
3
+ import 'react-dom';
4
+ export { useDebounce } from './hooks/useDebounce.js';
5
+ export { useThrottle } from './hooks/useThrottle.js';
6
+ export { Tooltip } from './Tooltip/Tooltip.js';
7
+ export { useSelectContext } from './Select/Select.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jy-headless",
3
- "version": "0.3.0",
3
+ "version": "0.3.7",
4
4
  "description": "A lightweight and customizable headless UI library for React components",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/yCZwIqY/jy-headless",
@@ -13,20 +13,45 @@
13
13
  "require": "./cjs/index.js",
14
14
  "types": "./index.d.ts"
15
15
  },
16
+ "./Popover": {
17
+ "import": "./cjs/Popover/Popover.js",
18
+ "require": "./cjs/cjs/Popover/Popover.js",
19
+ "types": "./cjs/Popover/Popover.d.ts"
20
+ },
21
+ "./Select": {
22
+ "import": "./cjs/Select/Select.js",
23
+ "require": "./cjs/cjs/Select/Select.js",
24
+ "types": "./cjs/Select/Select.d.ts"
25
+ },
26
+ "./Tooltip": {
27
+ "import": "./cjs/Tooltip/Tooltip.js",
28
+ "require": "./cjs/cjs/Tooltip/Tooltip.js",
29
+ "types": "./cjs/Tooltip/Tooltip.d.ts"
30
+ },
16
31
  "./cjs": {
17
32
  "import": "./cjs/index.js",
18
33
  "require": "./cjs/cjs/index.js",
19
34
  "types": "./cjs/index.d.ts"
20
35
  },
36
+ "./useDebounce": {
37
+ "import": "./hooks/useDebounce.js",
38
+ "require": "./cjs/hooks/useDebounce.js",
39
+ "types": "./hooks/useDebounce.d.ts"
40
+ },
41
+ "./usePortal": {
42
+ "import": "./hooks/usePortal.js",
43
+ "require": "./cjs/hooks/usePortal.js",
44
+ "types": "./hooks/usePortal.d.ts"
45
+ },
46
+ "./useThrottle": {
47
+ "import": "./hooks/useThrottle.js",
48
+ "require": "./cjs/hooks/useThrottle.js",
49
+ "types": "./hooks/useThrottle.d.ts"
50
+ },
21
51
  "./index": {
22
52
  "import": "./index.js",
23
53
  "require": "./cjs/index.js",
24
54
  "types": "./index.d.ts"
25
- },
26
- "./Popover": {
27
- "import": "./Popover/Popover.js",
28
- "require": "./cjs/Popover/Popover.js",
29
- "types": "./Popover/Popover.d.ts"
30
55
  }
31
56
  },
32
57
  "keywords": [
package/version.txt CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.7