cynx-ui 1.2.35 → 1.2.36

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.
@@ -1,4 +1,4 @@
1
- import { useState, useRef, useEffect } from 'react';
1
+ import { useState, useRef, useEffect, useLayoutEffect, useCallback } from 'react';
2
2
  import { createPortal } from 'react-dom';
3
3
  import { ChevronLeft, ChevronRight, Calendar } from 'lucide-react';
4
4
 
@@ -58,20 +58,44 @@ export function DatePicker({ value, onChange, placeholder = 'Select date', badge
58
58
  if (d) setCursor(d);
59
59
  }, [value]);
60
60
 
61
- // Compute button position when dropdown opens
62
- useEffect(() => {
63
- if (!open || !btnRef.current) return;
64
- function updatePos() {
65
- if (btnRef.current) {
66
- const r = btnRef.current.getBoundingClientRect();
67
- setPos({ top: r.bottom + 6, left: r.left, width: Math.max(r.width, 252) });
68
- }
61
+ const calcPos = useCallback(() => {
62
+ if (!btnRef.current) return;
63
+ const r = btnRef.current.getBoundingClientRect();
64
+ const dropW = Math.max(r.width, 260);
65
+ const dropH = dropRef.current ? dropRef.current.offsetHeight : 280;
66
+
67
+ let left = r.left;
68
+ if (left + dropW > window.innerWidth - 12) {
69
+ left = Math.max(12, r.right - dropW);
69
70
  }
70
- updatePos();
71
- window.addEventListener('scroll', updatePos, true);
72
- window.addEventListener('resize', updatePos);
73
- return () => { window.removeEventListener('scroll', updatePos, true); window.removeEventListener('resize', updatePos); };
74
- }, [open]);
71
+ if (left < 12) left = 12;
72
+
73
+ let top = r.bottom + 6;
74
+ if (top + dropH > window.innerHeight - 12 && r.top - 6 - dropH > 12) {
75
+ top = r.top - 6 - dropH;
76
+ }
77
+
78
+ setPos({ top, left, width: dropW });
79
+ }, []);
80
+
81
+ useLayoutEffect(() => {
82
+ if (!open) return;
83
+ calcPos();
84
+ window.addEventListener('scroll', calcPos, true);
85
+ window.addEventListener('resize', calcPos);
86
+ return () => {
87
+ window.removeEventListener('scroll', calcPos, true);
88
+ window.removeEventListener('resize', calcPos);
89
+ };
90
+ }, [open, calcPos]);
91
+
92
+ const toggleOpen = () => {
93
+ if (disabled) return;
94
+ if (!open) {
95
+ calcPos();
96
+ }
97
+ setOpen(o => !o);
98
+ };
75
99
 
76
100
  function pick(date) {
77
101
  if (typeof value === 'string') {
@@ -240,7 +264,7 @@ export function DatePicker({ value, onChange, placeholder = 'Select date', badge
240
264
  return (
241
265
  <div ref={ref} style={{ position: 'relative', display: 'inline-block', ...style }}>
242
266
  {/* Trigger */}
243
- <button ref={btnRef} type="button" onClick={() => !disabled && setOpen(o => !o)} style={{
267
+ <button ref={btnRef} type="button" onClick={toggleOpen} style={{
244
268
  display: 'flex', alignItems: 'center', gap: 7,
245
269
  padding: '5px 10px', borderRadius: 'var(--rads)',
246
270
  border: '1px solid var(--border)', background: 'var(--surface)',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cynx-ui",
3
- "version": "1.2.35",
3
+ "version": "1.2.36",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.js"