@betterinternship/components 3.3.3 → 3.3.4

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.
@@ -12,13 +12,35 @@ function TabGroup({ children, value, onValueChange, defaultValue, unmountInactiv
12
12
  const generatedId = React.useId();
13
13
  const id = providedId ?? generatedId;
14
14
  const reducedMotion = useReducedMotion();
15
+ const listRef = React.useRef(null);
16
+ const activeIndex = tabs.findIndex(tab => tab.props.name === active);
17
+ // The underline is placed from the active tab's offset inside the tab bar
18
+ // (not a shared layout animation, which compares screen positions and so
19
+ // flies across the page when the page height or scroll changes under it).
20
+ const [bar, setBar] = React.useState(null);
21
+ React.useLayoutEffect(() => {
22
+ const list = listRef.current;
23
+ if (!list)
24
+ return;
25
+ const measure = () => {
26
+ const tab = list.querySelectorAll('[role="tab"]')[activeIndex];
27
+ if (!tab)
28
+ return;
29
+ setBar(prev => prev && prev.left === tab.offsetLeft && prev.width === tab.offsetWidth ? prev : { left: tab.offsetLeft, width: tab.offsetWidth });
30
+ };
31
+ measure();
32
+ const observer = new ResizeObserver(measure);
33
+ observer.observe(list);
34
+ list.querySelectorAll('[role="tab"]').forEach(tab => observer.observe(tab));
35
+ return () => observer.disconnect();
36
+ }, [activeIndex, tabs.length]);
15
37
  const select = (tab) => {
16
38
  if (value === undefined)
17
39
  setInternal(tab.props.name);
18
40
  onValueChange?.(tab.props.name);
19
41
  tab.props.onTabChange?.();
20
42
  };
21
- return jsxs("div", { className: cn('w-full min-w-0', className), children: [jsx("div", { role: "tablist", "aria-label": label, className: "flex gap-1 overflow-x-auto", onKeyDown: event => {
43
+ return jsxs("div", { className: cn('w-full min-w-0', className), children: [jsxs("div", { ref: listRef, role: "tablist", "aria-label": label, className: "relative flex gap-1 overflow-x-auto", onKeyDown: event => {
22
44
  if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key))
23
45
  return;
24
46
  event.preventDefault();
@@ -28,7 +50,7 @@ function TabGroup({ children, value, onValueChange, defaultValue, unmountInactiv
28
50
  return;
29
51
  select(tabs[next]);
30
52
  event.currentTarget.querySelectorAll('[role="tab"]')[next]?.focus();
31
- }, children: tabs.map((tab, index) => jsxs("button", { type: "button", role: "tab", id: `${id}-tab-${index}`, "aria-controls": `${id}-panel-${index}`, "aria-selected": active === tab.props.name, tabIndex: active === tab.props.name ? 0 : -1, onClick: () => select(tab), className: cn('relative shrink-0 cursor-pointer px-4 py-3 text-sm text-muted-foreground transition-colors duration-state hover:text-foreground focus-visible:outline-3 focus-visible:-outline-offset-4 focus-visible:outline-landing-blue motion-reduce:transition-none', active === tab.props.name && 'font-semibold text-foreground'), children: [jsxs("span", { className: "inline-flex items-center gap-1.5", children: [tab.props.indicator && jsx("span", { "aria-label": "New activity", role: "img", className: "size-2.5 rounded-full bg-warning ring-2 ring-background" }), tab.props.name] }), active === tab.props.name && jsx(motion.span, { "aria-hidden": "true", layoutId: `${id}-underline`, className: "absolute inset-x-0 bottom-0 h-0.5 bg-primary", transition: reducedMotion ? { duration: 0 } : { type: 'spring', stiffness: 450, damping: 45 } })] }, tab.props.name)) }), !navigationOnly && tabs.map((tab, index) => {
53
+ }, children: [tabs.map((tab, index) => jsx("button", { type: "button", role: "tab", id: `${id}-tab-${index}`, "aria-controls": `${id}-panel-${index}`, "aria-selected": active === tab.props.name, tabIndex: active === tab.props.name ? 0 : -1, onClick: () => select(tab), className: cn('relative shrink-0 cursor-pointer px-4 py-3 text-sm text-muted-foreground transition-colors duration-state hover:text-foreground focus-visible:outline-3 focus-visible:-outline-offset-4 focus-visible:outline-landing-blue motion-reduce:transition-none', active === tab.props.name && 'font-semibold text-foreground'), children: jsxs("span", { className: "inline-flex items-center gap-1.5", children: [tab.props.indicator && jsx("span", { "aria-label": "New activity", role: "img", className: "size-2.5 rounded-full bg-warning ring-2 ring-background" }), tab.props.name] }) }, tab.props.name)), bar && jsx(motion.span, { "aria-hidden": "true", className: "pointer-events-none absolute bottom-0 left-0 h-0.5 bg-primary", initial: false, animate: { x: bar.left, width: bar.width }, transition: reducedMotion ? { duration: 0 } : { type: 'spring', stiffness: 450, damping: 45 } })] }), !navigationOnly && tabs.map((tab, index) => {
32
54
  const selected = active === tab.props.name;
33
55
  if (unmountInactive && !selected)
34
56
  return null;
@@ -1 +1 @@
1
- {"version":3,"file":"tabs.js","sources":["../../src/components/tabs.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from 'react';\nimport { motion, useReducedMotion } from 'framer-motion';\nimport { cn } from '@/utils';\n\ninterface TabProps {\n name: string;\n children: React.ReactNode;\n indicator?: boolean;\n onTabChange?: () => void;\n}\nexport const Tab = ({ children }: TabProps) => <>{children}</>;\n\ninterface TabGroupProps {\n children: React.ReactElement<TabProps> | (React.ReactElement<TabProps> | false | null | undefined)[];\n value?: string;\n onValueChange?: (value: string) => void;\n defaultValue?: string;\n unmountInactive?: boolean;\n className?: string;\n /** Render only the tab bar when panels are laid out elsewhere. */\n navigationOnly?: boolean;\n id?: string;\n 'aria-label'?: string;\n}\n\nexport function TabGroup({ children, value, onValueChange, defaultValue, unmountInactive = true, className, navigationOnly = false, id: providedId, 'aria-label': label = 'Sections' }: TabGroupProps) {\n const tabs = React.Children.toArray(children).filter(React.isValidElement) as React.ReactElement<TabProps>[];\n const [internal, setInternal] = React.useState(defaultValue ?? tabs[0]?.props.name ?? '');\n const active = value ?? (tabs.some(tab => tab.props.name === internal) ? internal : tabs[0]?.props.name);\n const generatedId = React.useId();\n const id = providedId ?? generatedId;\n const reducedMotion = useReducedMotion();\n const select = (tab: React.ReactElement<TabProps>) => {\n if (value === undefined) setInternal(tab.props.name);\n onValueChange?.(tab.props.name);\n tab.props.onTabChange?.();\n };\n return <div className={cn('w-full min-w-0', className)}>\n <div role=\"tablist\" aria-label={label} className=\"flex gap-1 overflow-x-auto\" onKeyDown={event => {\n if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;\n event.preventDefault();\n const index = tabs.findIndex(tab => tab.props.name === active);\n const next = event.key === 'Home' ? 0 : event.key === 'End' ? tabs.length - 1 : (index + (event.key === 'ArrowRight' ? 1 : -1) + tabs.length) % tabs.length;\n if (!tabs[next]) return;\n select(tabs[next]);\n event.currentTarget.querySelectorAll<HTMLButtonElement>('[role=\"tab\"]')[next]?.focus();\n }}>\n {tabs.map((tab, index) => <button key={tab.props.name} type=\"button\" role=\"tab\" id={`${id}-tab-${index}`} aria-controls={`${id}-panel-${index}`} aria-selected={active === tab.props.name} tabIndex={active === tab.props.name ? 0 : -1} onClick={() => select(tab)} className={cn('relative shrink-0 cursor-pointer px-4 py-3 text-sm text-muted-foreground transition-colors duration-state hover:text-foreground focus-visible:outline-3 focus-visible:-outline-offset-4 focus-visible:outline-landing-blue motion-reduce:transition-none', active === tab.props.name && 'font-semibold text-foreground')}>\n <span className=\"inline-flex items-center gap-1.5\">{tab.props.indicator && <span aria-label=\"New activity\" role=\"img\" className=\"size-2.5 rounded-full bg-warning ring-2 ring-background\" />}{tab.props.name}</span>\n {active === tab.props.name && <motion.span aria-hidden=\"true\" layoutId={`${id}-underline`} className=\"absolute inset-x-0 bottom-0 h-0.5 bg-primary\" transition={reducedMotion ? { duration: 0 } : { type: 'spring', stiffness: 450, damping: 45 }} />}\n </button>)}\n </div>\n {!navigationOnly && tabs.map((tab, index) => {\n const selected = active === tab.props.name;\n if (unmountInactive && !selected) return null;\n return <div key={tab.props.name} role=\"tabpanel\" id={`${id}-panel-${index}`} aria-labelledby={`${id}-tab-${index}`} hidden={!selected} tabIndex={0}>\n <motion.div initial={false} animate={{ opacity: selected ? 1 : 0, y: selected ? 0 : 4 }} transition={{ duration: reducedMotion ? 0 : 0.18 }}>{tab}</motion.div>\n </div>;\n })}\n </div>;\n}\n"],"names":[],"mappings":";;;;;;AAYO;AAeD;AACJ;;AAEA;AACA;AACA;AACA;AACA;;AAC2B;;AAEzB;AACF;;AAGI;;;AAEA;;AAEA;;AACA;AACA;;;;AASkC;AAClC;;AAKN;;"}
1
+ {"version":3,"file":"tabs.js","sources":["../../src/components/tabs.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from 'react';\nimport { motion, useReducedMotion } from 'framer-motion';\nimport { cn } from '@/utils';\n\ninterface TabProps {\n name: string;\n children: React.ReactNode;\n indicator?: boolean;\n onTabChange?: () => void;\n}\nexport const Tab = ({ children }: TabProps) => <>{children}</>;\n\ninterface TabGroupProps {\n children: React.ReactElement<TabProps> | (React.ReactElement<TabProps> | false | null | undefined)[];\n value?: string;\n onValueChange?: (value: string) => void;\n defaultValue?: string;\n unmountInactive?: boolean;\n className?: string;\n /** Render only the tab bar when panels are laid out elsewhere. */\n navigationOnly?: boolean;\n id?: string;\n 'aria-label'?: string;\n}\n\nexport function TabGroup({ children, value, onValueChange, defaultValue, unmountInactive = true, className, navigationOnly = false, id: providedId, 'aria-label': label = 'Sections' }: TabGroupProps) {\n const tabs = React.Children.toArray(children).filter(React.isValidElement) as React.ReactElement<TabProps>[];\n const [internal, setInternal] = React.useState(defaultValue ?? tabs[0]?.props.name ?? '');\n const active = value ?? (tabs.some(tab => tab.props.name === internal) ? internal : tabs[0]?.props.name);\n const generatedId = React.useId();\n const id = providedId ?? generatedId;\n const reducedMotion = useReducedMotion();\n const listRef = React.useRef<HTMLDivElement>(null);\n const activeIndex = tabs.findIndex(tab => tab.props.name === active);\n // The underline is placed from the active tab's offset inside the tab bar\n // (not a shared layout animation, which compares screen positions and so\n // flies across the page when the page height or scroll changes under it).\n const [bar, setBar] = React.useState<{ left: number; width: number } | null>(null);\n React.useLayoutEffect(() => {\n const list = listRef.current;\n if (!list) return;\n const measure = () => {\n const tab = list.querySelectorAll<HTMLElement>('[role=\"tab\"]')[activeIndex];\n if (!tab) return;\n setBar(prev => prev && prev.left === tab.offsetLeft && prev.width === tab.offsetWidth ? prev : { left: tab.offsetLeft, width: tab.offsetWidth });\n };\n measure();\n const observer = new ResizeObserver(measure);\n observer.observe(list);\n list.querySelectorAll('[role=\"tab\"]').forEach(tab => observer.observe(tab));\n return () => observer.disconnect();\n }, [activeIndex, tabs.length]);\n const select = (tab: React.ReactElement<TabProps>) => {\n if (value === undefined) setInternal(tab.props.name);\n onValueChange?.(tab.props.name);\n tab.props.onTabChange?.();\n };\n return <div className={cn('w-full min-w-0', className)}>\n <div ref={listRef} role=\"tablist\" aria-label={label} className=\"relative flex gap-1 overflow-x-auto\" onKeyDown={event => {\n if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;\n event.preventDefault();\n const index = tabs.findIndex(tab => tab.props.name === active);\n const next = event.key === 'Home' ? 0 : event.key === 'End' ? tabs.length - 1 : (index + (event.key === 'ArrowRight' ? 1 : -1) + tabs.length) % tabs.length;\n if (!tabs[next]) return;\n select(tabs[next]);\n event.currentTarget.querySelectorAll<HTMLButtonElement>('[role=\"tab\"]')[next]?.focus();\n }}>\n {tabs.map((tab, index) => <button key={tab.props.name} type=\"button\" role=\"tab\" id={`${id}-tab-${index}`} aria-controls={`${id}-panel-${index}`} aria-selected={active === tab.props.name} tabIndex={active === tab.props.name ? 0 : -1} onClick={() => select(tab)} className={cn('relative shrink-0 cursor-pointer px-4 py-3 text-sm text-muted-foreground transition-colors duration-state hover:text-foreground focus-visible:outline-3 focus-visible:-outline-offset-4 focus-visible:outline-landing-blue motion-reduce:transition-none', active === tab.props.name && 'font-semibold text-foreground')}>\n <span className=\"inline-flex items-center gap-1.5\">{tab.props.indicator && <span aria-label=\"New activity\" role=\"img\" className=\"size-2.5 rounded-full bg-warning ring-2 ring-background\" />}{tab.props.name}</span>\n </button>)}\n {bar && <motion.span aria-hidden=\"true\" className=\"pointer-events-none absolute bottom-0 left-0 h-0.5 bg-primary\" initial={false} animate={{ x: bar.left, width: bar.width }} transition={reducedMotion ? { duration: 0 } : { type: 'spring', stiffness: 450, damping: 45 }} />}\n </div>\n {!navigationOnly && tabs.map((tab, index) => {\n const selected = active === tab.props.name;\n if (unmountInactive && !selected) return null;\n return <div key={tab.props.name} role=\"tabpanel\" id={`${id}-panel-${index}`} aria-labelledby={`${id}-tab-${index}`} hidden={!selected} tabIndex={0}>\n <motion.div initial={false} animate={{ opacity: selected ? 1 : 0, y: selected ? 0 : 4 }} transition={{ duration: reducedMotion ? 0 : 0.18 }}>{tab}</motion.div>\n </div>;\n })}\n </div>;\n}\n"],"names":[],"mappings":";;;;;;AAYO;AAeD;AACJ;;AAEA;AACA;AACA;AACA;;AAEA;;;;AAIA;AACA;AACE;AACA;;;;AAGE;;AACA;AACF;AACA;AACA;AACA;AACA;AACA;;AAEF;;AAC2B;;AAEzB;AACF;AACA;AAEI;;;AAEA;;AAEA;;AACA;AACA;;;;AASkC;AAClC;;AAKN;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterinternship/components",
3
- "version": "3.3.3",
3
+ "version": "3.3.4",
4
4
  "workspaces": [
5
5
  "playground"
6
6
  ],