@classic-homes/theme-react 0.5.0 → 0.5.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/dist/index.d.mts CHANGED
@@ -498,6 +498,8 @@ interface StudioCanvasItemProps {
498
498
  invalid?: boolean;
499
499
  /** Where the "+" sits: centred on the item's bottom edge, or in the left gutter beside it */
500
500
  insertPlacement?: 'center' | 'gutter';
501
+ /** Toolbar straddling the top edge (default) or wholly above the item, for items edited in place. */
502
+ toolbarPlacement?: 'edge' | 'above';
501
503
  className?: string;
502
504
  }
503
505
  /**
@@ -508,7 +510,7 @@ interface StudioCanvasItemProps {
508
510
  * scrolls it into view; picking it on the canvas does not. `insertPlacement=
509
511
  * "gutter"` puts the "+" in the left margin, clear of text edited in place.
510
512
  */
511
- declare function StudioCanvasItem({ children, selected, onSelect, label, toolbar, onInsert, insertLabel, invalid, insertPlacement, className, }: StudioCanvasItemProps): React$1.JSX.Element;
513
+ declare function StudioCanvasItem({ children, selected, onSelect, label, toolbar, onInsert, insertLabel, invalid, insertPlacement, toolbarPlacement, className, }: StudioCanvasItemProps): React$1.JSX.Element;
512
514
 
513
515
  interface StudioInspectorTab {
514
516
  id: string;
package/dist/index.d.ts CHANGED
@@ -498,6 +498,8 @@ interface StudioCanvasItemProps {
498
498
  invalid?: boolean;
499
499
  /** Where the "+" sits: centred on the item's bottom edge, or in the left gutter beside it */
500
500
  insertPlacement?: 'center' | 'gutter';
501
+ /** Toolbar straddling the top edge (default) or wholly above the item, for items edited in place. */
502
+ toolbarPlacement?: 'edge' | 'above';
501
503
  className?: string;
502
504
  }
503
505
  /**
@@ -508,7 +510,7 @@ interface StudioCanvasItemProps {
508
510
  * scrolls it into view; picking it on the canvas does not. `insertPlacement=
509
511
  * "gutter"` puts the "+" in the left margin, clear of text edited in place.
510
512
  */
511
- declare function StudioCanvasItem({ children, selected, onSelect, label, toolbar, onInsert, insertLabel, invalid, insertPlacement, className, }: StudioCanvasItemProps): React$1.JSX.Element;
513
+ declare function StudioCanvasItem({ children, selected, onSelect, label, toolbar, onInsert, insertLabel, invalid, insertPlacement, toolbarPlacement, className, }: StudioCanvasItemProps): React$1.JSX.Element;
512
514
 
513
515
  interface StudioInspectorTab {
514
516
  id: string;
package/dist/index.js CHANGED
@@ -1800,6 +1800,7 @@ function StudioCanvasItem({
1800
1800
  insertLabel,
1801
1801
  invalid = false,
1802
1802
  insertPlacement = "center",
1803
+ toolbarPlacement = "edge",
1803
1804
  className
1804
1805
  }) {
1805
1806
  const studio = useStudio();
@@ -1859,7 +1860,10 @@ function StudioCanvasItem({
1859
1860
  "div",
1860
1861
  {
1861
1862
  "data-studio-quick-action": "",
1862
- className: "absolute -top-4 right-3 z-10 flex items-center gap-0.5 rounded-md border border-border bg-background p-0.5 shadow-md",
1863
+ className: cn(
1864
+ "absolute right-3 z-10 flex items-center gap-0.5 rounded-md border border-border bg-background p-0.5 shadow-md",
1865
+ toolbarPlacement === "above" ? "bottom-full mb-1" : "-top-4"
1866
+ ),
1863
1867
  children: toolbar
1864
1868
  }
1865
1869
  ),
@@ -2389,15 +2393,18 @@ var FOCUSABLE_SELECTOR = [
2389
2393
  "iframe",
2390
2394
  "object",
2391
2395
  "embed",
2392
- "[contenteditable]",
2396
+ '[contenteditable]:not([contenteditable="false"])',
2393
2397
  '[tabindex]:not([tabindex="-1"])'
2394
2398
  ].join(",");
2395
2399
  function getFocusableElements(container) {
2396
2400
  const elements = container.querySelectorAll(FOCUSABLE_SELECTOR);
2397
2401
  return Array.from(elements).filter(
2398
- (el) => el.offsetParent !== null && getComputedStyle(el).visibility !== "hidden"
2402
+ (el) => el.tabIndex >= 0 && !el.matches(":disabled") && !el.closest("[inert],[hidden]") && el.offsetParent !== null && getComputedStyle(el).visibility !== "hidden"
2399
2403
  );
2400
2404
  }
2405
+ function precedes(a, b) {
2406
+ return Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
2407
+ }
2401
2408
  function useFocusTrap(options = {}) {
2402
2409
  const { active = true, returnFocusTo, initialFocus, preventScroll = false } = options;
2403
2410
  const containerRef = (0, import_react.useRef)(null);
@@ -2407,17 +2414,28 @@ function useFocusTrap(options = {}) {
2407
2414
  if (!active || event.key !== "Tab") return;
2408
2415
  const container = containerRef.current;
2409
2416
  if (!container) return;
2417
+ const current = document.activeElement;
2418
+ if (!current || !container.contains(current)) return;
2410
2419
  const focusableElements = getFocusableElements(container);
2411
- if (focusableElements.length === 0) return;
2420
+ if (focusableElements.length === 0) {
2421
+ event.preventDefault();
2422
+ container.focus({ preventScroll });
2423
+ return;
2424
+ }
2412
2425
  const firstElement = focusableElements[0];
2413
2426
  const lastElement = focusableElements[focusableElements.length - 1];
2427
+ if (current === container) {
2428
+ event.preventDefault();
2429
+ (event.shiftKey ? lastElement : firstElement).focus({ preventScroll });
2430
+ return;
2431
+ }
2414
2432
  if (event.shiftKey) {
2415
- if (document.activeElement === firstElement) {
2433
+ if (current === firstElement || precedes(current, firstElement)) {
2416
2434
  event.preventDefault();
2417
2435
  lastElement.focus({ preventScroll });
2418
2436
  }
2419
2437
  } else {
2420
- if (document.activeElement === lastElement) {
2438
+ if (current === lastElement || precedes(lastElement, current)) {
2421
2439
  event.preventDefault();
2422
2440
  firstElement.focus({ preventScroll });
2423
2441
  }
@@ -2430,24 +2448,30 @@ function useFocusTrap(options = {}) {
2430
2448
  const container = containerRef.current;
2431
2449
  if (!container) return;
2432
2450
  previousActiveElementRef.current = document.activeElement;
2451
+ let addedTabIndex = false;
2433
2452
  const focusableElements = getFocusableElements(container);
2434
- let focusFrame = 0;
2435
- if (focusableElements.length > 0) {
2436
- let elementToFocus = null;
2437
- if (initialFocus) {
2438
- elementToFocus = container.querySelector(initialFocus);
2439
- }
2440
- if (!elementToFocus) {
2441
- elementToFocus = focusableElements[0];
2453
+ let elementToFocus = null;
2454
+ if (initialFocus) {
2455
+ elementToFocus = container.querySelector(initialFocus);
2456
+ }
2457
+ if (!elementToFocus) {
2458
+ elementToFocus = focusableElements[0] ?? null;
2459
+ }
2460
+ if (!elementToFocus) {
2461
+ if (!container.hasAttribute("tabindex")) {
2462
+ container.setAttribute("tabindex", "-1");
2463
+ addedTabIndex = true;
2442
2464
  }
2443
- focusFrame = requestAnimationFrame(() => {
2444
- elementToFocus?.focus({ preventScroll });
2445
- });
2465
+ elementToFocus = container;
2446
2466
  }
2467
+ const focusFrame = requestAnimationFrame(() => {
2468
+ elementToFocus?.focus({ preventScroll });
2469
+ });
2447
2470
  document.addEventListener("keydown", handleKeyDown);
2448
2471
  return () => {
2449
2472
  cancelAnimationFrame(focusFrame);
2450
2473
  document.removeEventListener("keydown", handleKeyDown);
2474
+ if (addedTabIndex) container.removeAttribute("tabindex");
2451
2475
  const elementToReturn = returnFocusTo ?? previousActiveElementRef.current;
2452
2476
  if (elementToReturn && typeof elementToReturn.focus === "function") {
2453
2477
  elementToReturn.focus({ preventScroll });
package/dist/index.mjs CHANGED
@@ -1690,6 +1690,7 @@ function StudioCanvasItem({
1690
1690
  insertLabel,
1691
1691
  invalid = false,
1692
1692
  insertPlacement = "center",
1693
+ toolbarPlacement = "edge",
1693
1694
  className
1694
1695
  }) {
1695
1696
  const studio = useStudio();
@@ -1749,7 +1750,10 @@ function StudioCanvasItem({
1749
1750
  "div",
1750
1751
  {
1751
1752
  "data-studio-quick-action": "",
1752
- className: "absolute -top-4 right-3 z-10 flex items-center gap-0.5 rounded-md border border-border bg-background p-0.5 shadow-md",
1753
+ className: cn(
1754
+ "absolute right-3 z-10 flex items-center gap-0.5 rounded-md border border-border bg-background p-0.5 shadow-md",
1755
+ toolbarPlacement === "above" ? "bottom-full mb-1" : "-top-4"
1756
+ ),
1753
1757
  children: toolbar
1754
1758
  }
1755
1759
  ),
@@ -2279,15 +2283,18 @@ var FOCUSABLE_SELECTOR = [
2279
2283
  "iframe",
2280
2284
  "object",
2281
2285
  "embed",
2282
- "[contenteditable]",
2286
+ '[contenteditable]:not([contenteditable="false"])',
2283
2287
  '[tabindex]:not([tabindex="-1"])'
2284
2288
  ].join(",");
2285
2289
  function getFocusableElements(container) {
2286
2290
  const elements = container.querySelectorAll(FOCUSABLE_SELECTOR);
2287
2291
  return Array.from(elements).filter(
2288
- (el) => el.offsetParent !== null && getComputedStyle(el).visibility !== "hidden"
2292
+ (el) => el.tabIndex >= 0 && !el.matches(":disabled") && !el.closest("[inert],[hidden]") && el.offsetParent !== null && getComputedStyle(el).visibility !== "hidden"
2289
2293
  );
2290
2294
  }
2295
+ function precedes(a, b) {
2296
+ return Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
2297
+ }
2291
2298
  function useFocusTrap(options = {}) {
2292
2299
  const { active = true, returnFocusTo, initialFocus, preventScroll = false } = options;
2293
2300
  const containerRef = useRef8(null);
@@ -2297,17 +2304,28 @@ function useFocusTrap(options = {}) {
2297
2304
  if (!active || event.key !== "Tab") return;
2298
2305
  const container = containerRef.current;
2299
2306
  if (!container) return;
2307
+ const current = document.activeElement;
2308
+ if (!current || !container.contains(current)) return;
2300
2309
  const focusableElements = getFocusableElements(container);
2301
- if (focusableElements.length === 0) return;
2310
+ if (focusableElements.length === 0) {
2311
+ event.preventDefault();
2312
+ container.focus({ preventScroll });
2313
+ return;
2314
+ }
2302
2315
  const firstElement = focusableElements[0];
2303
2316
  const lastElement = focusableElements[focusableElements.length - 1];
2317
+ if (current === container) {
2318
+ event.preventDefault();
2319
+ (event.shiftKey ? lastElement : firstElement).focus({ preventScroll });
2320
+ return;
2321
+ }
2304
2322
  if (event.shiftKey) {
2305
- if (document.activeElement === firstElement) {
2323
+ if (current === firstElement || precedes(current, firstElement)) {
2306
2324
  event.preventDefault();
2307
2325
  lastElement.focus({ preventScroll });
2308
2326
  }
2309
2327
  } else {
2310
- if (document.activeElement === lastElement) {
2328
+ if (current === lastElement || precedes(lastElement, current)) {
2311
2329
  event.preventDefault();
2312
2330
  firstElement.focus({ preventScroll });
2313
2331
  }
@@ -2320,24 +2338,30 @@ function useFocusTrap(options = {}) {
2320
2338
  const container = containerRef.current;
2321
2339
  if (!container) return;
2322
2340
  previousActiveElementRef.current = document.activeElement;
2341
+ let addedTabIndex = false;
2323
2342
  const focusableElements = getFocusableElements(container);
2324
- let focusFrame = 0;
2325
- if (focusableElements.length > 0) {
2326
- let elementToFocus = null;
2327
- if (initialFocus) {
2328
- elementToFocus = container.querySelector(initialFocus);
2329
- }
2330
- if (!elementToFocus) {
2331
- elementToFocus = focusableElements[0];
2343
+ let elementToFocus = null;
2344
+ if (initialFocus) {
2345
+ elementToFocus = container.querySelector(initialFocus);
2346
+ }
2347
+ if (!elementToFocus) {
2348
+ elementToFocus = focusableElements[0] ?? null;
2349
+ }
2350
+ if (!elementToFocus) {
2351
+ if (!container.hasAttribute("tabindex")) {
2352
+ container.setAttribute("tabindex", "-1");
2353
+ addedTabIndex = true;
2332
2354
  }
2333
- focusFrame = requestAnimationFrame(() => {
2334
- elementToFocus?.focus({ preventScroll });
2335
- });
2355
+ elementToFocus = container;
2336
2356
  }
2357
+ const focusFrame = requestAnimationFrame(() => {
2358
+ elementToFocus?.focus({ preventScroll });
2359
+ });
2337
2360
  document.addEventListener("keydown", handleKeyDown);
2338
2361
  return () => {
2339
2362
  cancelAnimationFrame(focusFrame);
2340
2363
  document.removeEventListener("keydown", handleKeyDown);
2364
+ if (addedTabIndex) container.removeAttribute("tabindex");
2341
2365
  const elementToReturn = returnFocusTo ?? previousActiveElementRef.current;
2342
2366
  if (elementToReturn && typeof elementToReturn.focus === "function") {
2343
2367
  elementToReturn.focus({ preventScroll });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/theme-react",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "React components for the Classic theme system",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",