@affinda/react 0.0.27 → 0.0.29

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.
@@ -26,14 +26,22 @@ export const AfAvatar = ({ src, alt = 'Avatar', initials, size = 'medium', statu
26
26
  width: dimension,
27
27
  height: dimension,
28
28
  borderRadius: '50%',
29
+ flexShrink: 0,
30
+ ...style,
31
+ };
32
+ const contentStyles = {
33
+ display: 'flex',
34
+ alignItems: 'center',
35
+ justifyContent: 'center',
36
+ width: '100%',
37
+ height: '100%',
38
+ borderRadius: '50%',
29
39
  background: 'var(--colour-brand-mist-green, #c6d5d1)',
30
40
  color: 'var(--colour-brand-inkwell, #14343b)',
31
41
  fontFamily: 'var(--typography-bodyfont, system-ui, sans-serif)',
32
42
  fontSize,
33
43
  fontWeight: 600,
34
44
  overflow: 'hidden',
35
- flexShrink: 0,
36
- ...style,
37
45
  };
38
46
  const imageStyles = {
39
47
  width: '100%',
@@ -50,5 +58,5 @@ export const AfAvatar = ({ src, alt = 'Avatar', initials, size = 'medium', statu
50
58
  background: status ? statusColors[status] : 'transparent',
51
59
  border: '2px solid var(--colour-background-white, #fff)',
52
60
  };
53
- return (_jsxs("div", { style: avatarStyles, children: [src ? (_jsx("img", { src: src, alt: alt, style: imageStyles })) : (_jsx("span", { children: initials || alt.charAt(0).toUpperCase() })), status && _jsx("div", { style: statusStyles, "aria-label": `Status: ${status}` })] }));
61
+ return (_jsxs("div", { style: avatarStyles, children: [_jsx("div", { style: contentStyles, children: src ? (_jsx("img", { src: src, alt: alt, style: imageStyles })) : (_jsx("span", { children: initials || alt.charAt(0).toUpperCase() })) }), status && _jsx("div", { style: statusStyles, "aria-label": `Status: ${status}` })] }));
54
62
  };
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useState, useRef, useEffect } from 'react';
2
+ import { useState, useRef, useEffect, useCallback } from 'react';
3
+ import { createPortal } from 'react-dom';
3
4
  /**
4
5
  * AfSelect - Draft dropdown select component.
5
6
  *
@@ -8,17 +9,46 @@ import { useState, useRef, useEffect } from 'react';
8
9
  export const AfSelect = ({ options, value, placeholder = 'Select an option', label, description, error, disabled = false, required = false, onChange, style, }) => {
9
10
  const [isOpen, setIsOpen] = useState(false);
10
11
  const [selectedValue, setSelectedValue] = useState(value || '');
12
+ const [dropdownPosition, setDropdownPosition] = useState(null);
11
13
  const containerRef = useRef(null);
14
+ const triggerRef = useRef(null);
15
+ const dropdownRef = useRef(null);
12
16
  const selectedOption = options.find(opt => opt.value === selectedValue);
17
+ const updateDropdownPosition = useCallback(() => {
18
+ if (triggerRef.current) {
19
+ const rect = triggerRef.current.getBoundingClientRect();
20
+ setDropdownPosition({
21
+ top: rect.bottom + window.scrollY + 4,
22
+ left: rect.left + window.scrollX,
23
+ width: rect.width,
24
+ });
25
+ }
26
+ }, []);
13
27
  useEffect(() => {
14
28
  const handleClickOutside = (event) => {
15
- if (containerRef.current && !containerRef.current.contains(event.target)) {
29
+ const target = event.target;
30
+ const clickedInsideContainer = containerRef.current?.contains(target);
31
+ const clickedInsideDropdown = dropdownRef.current?.contains(target);
32
+ if (!clickedInsideContainer && !clickedInsideDropdown) {
16
33
  setIsOpen(false);
17
34
  }
18
35
  };
19
36
  document.addEventListener('mousedown', handleClickOutside);
20
37
  return () => document.removeEventListener('mousedown', handleClickOutside);
21
38
  }, []);
39
+ // Update dropdown position when opening or on scroll/resize
40
+ useEffect(() => {
41
+ if (!isOpen)
42
+ return;
43
+ updateDropdownPosition();
44
+ const handleScrollOrResize = () => updateDropdownPosition();
45
+ window.addEventListener('scroll', handleScrollOrResize, true);
46
+ window.addEventListener('resize', handleScrollOrResize);
47
+ return () => {
48
+ window.removeEventListener('scroll', handleScrollOrResize, true);
49
+ window.removeEventListener('resize', handleScrollOrResize);
50
+ };
51
+ }, [isOpen, updateDropdownPosition]);
22
52
  const handleSelect = (optionValue) => {
23
53
  setSelectedValue(optionValue);
24
54
  setIsOpen(false);
@@ -43,29 +73,32 @@ export const AfSelect = ({ options, value, placeholder = 'Select an option', lab
43
73
  boxSizing: 'border-box',
44
74
  };
45
75
  const dropdownStyles = {
46
- position: 'absolute',
47
- top: '100%',
48
- left: 0,
49
- right: 0,
50
- marginTop: '4px',
51
76
  background: 'var(--colour-background-white, #fff)',
52
77
  border: '1px solid var(--colour-tints-inkwell-100, #e0e5e6)',
53
78
  borderRadius: '8px',
54
79
  boxShadow: '0 4px 12px rgba(0, 0, 0, 0.1)',
55
- zIndex: 100,
56
80
  maxHeight: '240px',
57
81
  overflow: 'auto',
82
+ margin: 0,
83
+ padding: 0,
84
+ position: 'fixed',
85
+ top: dropdownPosition ? dropdownPosition.top - window.scrollY : 0,
86
+ left: dropdownPosition?.left ?? 0,
87
+ width: dropdownPosition?.width ?? 'auto',
88
+ zIndex: 9999,
58
89
  };
59
- return (_jsxs("div", { ref: containerRef, style: { position: 'relative', ...baseStyles, ...style }, children: [label && (_jsxs("label", { style: { display: 'block', marginBottom: '4px', fontWeight: 500, color: 'var(--colour-brand-inkwell, #14343b)' }, children: [label, required && _jsx("span", { style: { color: 'var(--colour-feedback-error, #dc3545)' }, children: " *" })] })), description && (_jsx("p", { style: { margin: '0 0 8px', fontSize: '14px', color: 'var(--colour-tints-inkwell-400, #7a8a8d)' }, children: description })), _jsxs("button", { type: "button", onClick: () => !disabled && setIsOpen(!isOpen), style: triggerStyles, disabled: disabled, "aria-haspopup": "listbox", "aria-expanded": isOpen, children: [_jsx("span", { children: selectedOption?.label || placeholder }), _jsx("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", style: { transform: isOpen ? 'rotate(180deg)' : 'rotate(0)', transition: 'transform 0.2s' }, children: _jsx("path", { d: "M6 9L12 15L18 9", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" }) })] }), isOpen && (_jsx("ul", { style: dropdownStyles, role: "listbox", children: options.map((option) => (_jsx("li", { role: "option", "aria-selected": option.value === selectedValue, onClick: () => !option.disabled && handleSelect(option.value), style: {
60
- padding: '12px 16px',
61
- cursor: option.disabled ? 'not-allowed' : 'pointer',
62
- opacity: option.disabled ? 0.5 : 1,
63
- background: option.value === selectedValue ? 'var(--colour-tints-mist-green-100, #e8f0ee)' : 'transparent',
64
- listStyle: 'none',
65
- }, onMouseEnter: (e) => {
66
- if (!option.disabled)
67
- e.currentTarget.style.background = 'var(--colour-tints-mist-green-50, #f4f8f7)';
68
- }, onMouseLeave: (e) => {
69
- e.currentTarget.style.background = option.value === selectedValue ? 'var(--colour-tints-mist-green-100, #e8f0ee)' : 'transparent';
70
- }, children: option.label }, option.value))) })), error && (_jsx("p", { style: { margin: '4px 0 0', fontSize: '14px', color: 'var(--colour-feedback-error, #dc3545)' }, children: error }))] }));
90
+ const dropdownContent = (_jsx("ul", { ref: dropdownRef, style: dropdownStyles, role: "listbox", children: options.map((option) => (_jsx("li", { role: "option", "aria-selected": option.value === selectedValue, onClick: () => !option.disabled && handleSelect(option.value), style: {
91
+ padding: '12px 16px',
92
+ cursor: option.disabled ? 'not-allowed' : 'pointer',
93
+ opacity: option.disabled ? 0.5 : 1,
94
+ background: option.value === selectedValue ? 'var(--colour-tints-mist-green-100, #e8f0ee)' : 'transparent',
95
+ listStyle: 'none',
96
+ }, onMouseEnter: (e) => {
97
+ if (!option.disabled)
98
+ e.currentTarget.style.background = 'var(--colour-tints-mist-green-50, #f4f8f7)';
99
+ }, onMouseLeave: (e) => {
100
+ e.currentTarget.style.background = option.value === selectedValue ? 'var(--colour-tints-mist-green-100, #e8f0ee)' : 'transparent';
101
+ }, children: option.label }, option.value))) }));
102
+ const renderDropdown = () => createPortal(dropdownContent, document.body);
103
+ return (_jsxs("div", { ref: containerRef, style: { position: 'relative', ...baseStyles, ...style }, children: [label && (_jsxs("label", { style: { display: 'block', marginBottom: '4px', fontWeight: 500, color: 'var(--colour-brand-inkwell, #14343b)' }, children: [label, required && _jsx("span", { style: { color: 'var(--colour-feedback-error, #dc3545)' }, children: " *" })] })), description && (_jsx("p", { style: { margin: '0 0 8px', fontSize: '14px', color: 'var(--colour-tints-inkwell-400, #7a8a8d)' }, children: description })), _jsxs("button", { ref: triggerRef, type: "button", onClick: () => !disabled && setIsOpen(!isOpen), style: triggerStyles, disabled: disabled, "aria-haspopup": "listbox", "aria-expanded": isOpen, children: [_jsx("span", { children: selectedOption?.label || placeholder }), _jsx("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", style: { transform: isOpen ? 'rotate(180deg)' : 'rotate(0)', transition: 'transform 0.2s' }, children: _jsx("path", { d: "M6 9L12 15L18 9", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" }) })] }), isOpen && renderDropdown(), error && (_jsx("p", { style: { margin: '4px 0 0', fontSize: '14px', color: 'var(--colour-feedback-error, #dc3545)' }, children: error }))] }));
71
104
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@affinda/react",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "files": [
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@affinda/icons": "^0.0.6",
19
- "@affinda/wc": "^0.0.17",
19
+ "@affinda/wc": "^0.0.20",
20
20
  "@stencil/react-output-target": "^1.2.0"
21
21
  },
22
22
  "devDependencies": {