@classic-homes/theme-react 0.5.1 → 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.js CHANGED
@@ -2393,15 +2393,18 @@ var FOCUSABLE_SELECTOR = [
2393
2393
  "iframe",
2394
2394
  "object",
2395
2395
  "embed",
2396
- "[contenteditable]",
2396
+ '[contenteditable]:not([contenteditable="false"])',
2397
2397
  '[tabindex]:not([tabindex="-1"])'
2398
2398
  ].join(",");
2399
2399
  function getFocusableElements(container) {
2400
2400
  const elements = container.querySelectorAll(FOCUSABLE_SELECTOR);
2401
2401
  return Array.from(elements).filter(
2402
- (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"
2403
2403
  );
2404
2404
  }
2405
+ function precedes(a, b) {
2406
+ return Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
2407
+ }
2405
2408
  function useFocusTrap(options = {}) {
2406
2409
  const { active = true, returnFocusTo, initialFocus, preventScroll = false } = options;
2407
2410
  const containerRef = (0, import_react.useRef)(null);
@@ -2411,17 +2414,28 @@ function useFocusTrap(options = {}) {
2411
2414
  if (!active || event.key !== "Tab") return;
2412
2415
  const container = containerRef.current;
2413
2416
  if (!container) return;
2417
+ const current = document.activeElement;
2418
+ if (!current || !container.contains(current)) return;
2414
2419
  const focusableElements = getFocusableElements(container);
2415
- if (focusableElements.length === 0) return;
2420
+ if (focusableElements.length === 0) {
2421
+ event.preventDefault();
2422
+ container.focus({ preventScroll });
2423
+ return;
2424
+ }
2416
2425
  const firstElement = focusableElements[0];
2417
2426
  const lastElement = focusableElements[focusableElements.length - 1];
2427
+ if (current === container) {
2428
+ event.preventDefault();
2429
+ (event.shiftKey ? lastElement : firstElement).focus({ preventScroll });
2430
+ return;
2431
+ }
2418
2432
  if (event.shiftKey) {
2419
- if (document.activeElement === firstElement) {
2433
+ if (current === firstElement || precedes(current, firstElement)) {
2420
2434
  event.preventDefault();
2421
2435
  lastElement.focus({ preventScroll });
2422
2436
  }
2423
2437
  } else {
2424
- if (document.activeElement === lastElement) {
2438
+ if (current === lastElement || precedes(lastElement, current)) {
2425
2439
  event.preventDefault();
2426
2440
  firstElement.focus({ preventScroll });
2427
2441
  }
@@ -2434,24 +2448,30 @@ function useFocusTrap(options = {}) {
2434
2448
  const container = containerRef.current;
2435
2449
  if (!container) return;
2436
2450
  previousActiveElementRef.current = document.activeElement;
2451
+ let addedTabIndex = false;
2437
2452
  const focusableElements = getFocusableElements(container);
2438
- let focusFrame = 0;
2439
- if (focusableElements.length > 0) {
2440
- let elementToFocus = null;
2441
- if (initialFocus) {
2442
- elementToFocus = container.querySelector(initialFocus);
2443
- }
2444
- if (!elementToFocus) {
2445
- 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;
2446
2464
  }
2447
- focusFrame = requestAnimationFrame(() => {
2448
- elementToFocus?.focus({ preventScroll });
2449
- });
2465
+ elementToFocus = container;
2450
2466
  }
2467
+ const focusFrame = requestAnimationFrame(() => {
2468
+ elementToFocus?.focus({ preventScroll });
2469
+ });
2451
2470
  document.addEventListener("keydown", handleKeyDown);
2452
2471
  return () => {
2453
2472
  cancelAnimationFrame(focusFrame);
2454
2473
  document.removeEventListener("keydown", handleKeyDown);
2474
+ if (addedTabIndex) container.removeAttribute("tabindex");
2455
2475
  const elementToReturn = returnFocusTo ?? previousActiveElementRef.current;
2456
2476
  if (elementToReturn && typeof elementToReturn.focus === "function") {
2457
2477
  elementToReturn.focus({ preventScroll });
package/dist/index.mjs CHANGED
@@ -2283,15 +2283,18 @@ var FOCUSABLE_SELECTOR = [
2283
2283
  "iframe",
2284
2284
  "object",
2285
2285
  "embed",
2286
- "[contenteditable]",
2286
+ '[contenteditable]:not([contenteditable="false"])',
2287
2287
  '[tabindex]:not([tabindex="-1"])'
2288
2288
  ].join(",");
2289
2289
  function getFocusableElements(container) {
2290
2290
  const elements = container.querySelectorAll(FOCUSABLE_SELECTOR);
2291
2291
  return Array.from(elements).filter(
2292
- (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"
2293
2293
  );
2294
2294
  }
2295
+ function precedes(a, b) {
2296
+ return Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
2297
+ }
2295
2298
  function useFocusTrap(options = {}) {
2296
2299
  const { active = true, returnFocusTo, initialFocus, preventScroll = false } = options;
2297
2300
  const containerRef = useRef8(null);
@@ -2301,17 +2304,28 @@ function useFocusTrap(options = {}) {
2301
2304
  if (!active || event.key !== "Tab") return;
2302
2305
  const container = containerRef.current;
2303
2306
  if (!container) return;
2307
+ const current = document.activeElement;
2308
+ if (!current || !container.contains(current)) return;
2304
2309
  const focusableElements = getFocusableElements(container);
2305
- if (focusableElements.length === 0) return;
2310
+ if (focusableElements.length === 0) {
2311
+ event.preventDefault();
2312
+ container.focus({ preventScroll });
2313
+ return;
2314
+ }
2306
2315
  const firstElement = focusableElements[0];
2307
2316
  const lastElement = focusableElements[focusableElements.length - 1];
2317
+ if (current === container) {
2318
+ event.preventDefault();
2319
+ (event.shiftKey ? lastElement : firstElement).focus({ preventScroll });
2320
+ return;
2321
+ }
2308
2322
  if (event.shiftKey) {
2309
- if (document.activeElement === firstElement) {
2323
+ if (current === firstElement || precedes(current, firstElement)) {
2310
2324
  event.preventDefault();
2311
2325
  lastElement.focus({ preventScroll });
2312
2326
  }
2313
2327
  } else {
2314
- if (document.activeElement === lastElement) {
2328
+ if (current === lastElement || precedes(lastElement, current)) {
2315
2329
  event.preventDefault();
2316
2330
  firstElement.focus({ preventScroll });
2317
2331
  }
@@ -2324,24 +2338,30 @@ function useFocusTrap(options = {}) {
2324
2338
  const container = containerRef.current;
2325
2339
  if (!container) return;
2326
2340
  previousActiveElementRef.current = document.activeElement;
2341
+ let addedTabIndex = false;
2327
2342
  const focusableElements = getFocusableElements(container);
2328
- let focusFrame = 0;
2329
- if (focusableElements.length > 0) {
2330
- let elementToFocus = null;
2331
- if (initialFocus) {
2332
- elementToFocus = container.querySelector(initialFocus);
2333
- }
2334
- if (!elementToFocus) {
2335
- 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;
2336
2354
  }
2337
- focusFrame = requestAnimationFrame(() => {
2338
- elementToFocus?.focus({ preventScroll });
2339
- });
2355
+ elementToFocus = container;
2340
2356
  }
2357
+ const focusFrame = requestAnimationFrame(() => {
2358
+ elementToFocus?.focus({ preventScroll });
2359
+ });
2341
2360
  document.addEventListener("keydown", handleKeyDown);
2342
2361
  return () => {
2343
2362
  cancelAnimationFrame(focusFrame);
2344
2363
  document.removeEventListener("keydown", handleKeyDown);
2364
+ if (addedTabIndex) container.removeAttribute("tabindex");
2345
2365
  const elementToReturn = returnFocusTo ?? previousActiveElementRef.current;
2346
2366
  if (elementToReturn && typeof elementToReturn.focus === "function") {
2347
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.1",
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",