@dloizides/ui-layout 1.0.1 → 1.2.0

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.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import React, { useMemo, useRef, useEffect, useState, useCallback } from 'react';
2
- import { StyleSheet, View, Text, TouchableOpacity, Platform, Modal, FlatList } from 'react-native';
2
+ import { StyleSheet, View, Text, TouchableOpacity, Platform, Modal, FlatList, ScrollView, useWindowDimensions } from 'react-native';
3
3
  import { MODAL_OVERLAY_COLOR, useUi } from '@dloizides/ui-feedback';
4
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
+ import { jsx, jsxs } from 'react/jsx-runtime';
5
+ import { SvgIcon } from '@dloizides/ui-icons';
5
6
 
6
7
  // src/Section/Section.tsx
7
8
  var styles = StyleSheet.create({
@@ -26,7 +27,9 @@ var LAYOUT_TEST_IDS = {
26
27
  statusLabel: "status-label",
27
28
  upgradePrompt: "upgrade-prompt",
28
29
  upgradePromptCta: "upgrade-prompt-cta",
29
- upgradePromptDismiss: "upgrade-prompt-dismiss"
30
+ upgradePromptDismiss: "upgrade-prompt-dismiss",
31
+ modalShell: "template-modal",
32
+ modalShellClose: "cancel-button"
30
33
  };
31
34
  var DEFAULT_BILLING_ROUTE = "/settings/billing";
32
35
  var styles2 = StyleSheet.create({
@@ -207,24 +210,226 @@ function useFocusTrap(containerRef, enabled) {
207
210
  };
208
211
  }, [containerRef, enabled]);
209
212
  }
210
- var BORDER_RADIUS = 8;
211
- var BORDER_WIDTH = 1;
213
+
214
+ // src/ModalDropdown/DropdownVariant.ts
215
+ var DropdownVariant = /* @__PURE__ */ ((DropdownVariant2) => {
216
+ DropdownVariant2["Menu"] = "menu";
217
+ DropdownVariant2["Modal"] = "modal";
218
+ return DropdownVariant2;
219
+ })(DropdownVariant || {});
212
220
  var OPTION_BORDER_RADIUS = 4;
213
221
  var BODY_FONT_SIZE = 14;
214
222
  var SELECTED_FONT_WEIGHT = "600";
223
+ var OPTION_PADDING_H = 16;
224
+ var OPTION_PADDING_V = 12;
225
+ var styles5 = StyleSheet.create({
226
+ option: {
227
+ paddingHorizontal: OPTION_PADDING_H,
228
+ paddingVertical: OPTION_PADDING_V,
229
+ borderRadius: OPTION_BORDER_RADIUS
230
+ },
231
+ optionText: { fontSize: BODY_FONT_SIZE },
232
+ selectedText: { fontWeight: SELECTED_FONT_WEIGHT }
233
+ });
234
+ var OptionRow = ({
235
+ testID,
236
+ label,
237
+ isSelected,
238
+ isHighlighted = false,
239
+ onSelect
240
+ }) => {
241
+ const { theme, t } = useUi();
242
+ const { colors } = theme;
243
+ const handlePress = useCallback(() => {
244
+ onSelect();
245
+ }, [onSelect]);
246
+ const isActive = isSelected || isHighlighted;
247
+ const rowStyle = [styles5.option, isActive ? { backgroundColor: colors.surfaceElevated } : null];
248
+ const textStyle = [styles5.optionText, { color: colors.text }, isSelected ? styles5.selectedText : null];
249
+ return /* @__PURE__ */ jsx(
250
+ TouchableOpacity,
251
+ {
252
+ accessible: true,
253
+ accessibilityHint: t("common.selectOptionHint"),
254
+ accessibilityLabel: label,
255
+ accessibilityRole: "button",
256
+ "aria-selected": isSelected,
257
+ style: rowStyle,
258
+ testID,
259
+ onPress: handlePress,
260
+ children: /* @__PURE__ */ jsx(Text, { style: textStyle, children: label })
261
+ }
262
+ );
263
+ };
264
+ var clampIndex = (index, itemCount) => {
265
+ if (index < 0) return 0;
266
+ const lastIndex = itemCount - 1;
267
+ return index > lastIndex ? lastIndex : index;
268
+ };
269
+ function buildKeyHandler(params) {
270
+ const { itemCount, onHighlightChange, onSelectHighlighted, onClose } = params;
271
+ return (event) => {
272
+ switch (event.key) {
273
+ case "ArrowDown":
274
+ event.preventDefault();
275
+ onHighlightChange((prev) => clampIndex(prev + 1, itemCount));
276
+ return;
277
+ case "ArrowUp":
278
+ event.preventDefault();
279
+ onHighlightChange((prev) => clampIndex(prev - 1, itemCount));
280
+ return;
281
+ case "Home":
282
+ event.preventDefault();
283
+ onHighlightChange(() => 0);
284
+ return;
285
+ case "End":
286
+ event.preventDefault();
287
+ onHighlightChange(() => clampIndex(itemCount - 1, itemCount));
288
+ return;
289
+ case "Enter":
290
+ event.preventDefault();
291
+ onSelectHighlighted();
292
+ return;
293
+ case "Escape":
294
+ event.preventDefault();
295
+ onClose();
296
+ return;
297
+ }
298
+ };
299
+ }
300
+ function useMenuKeyboard(params) {
301
+ const { containerRef, enabled, onClose } = params;
302
+ useEffect(() => {
303
+ if (Platform.OS !== "web" || !enabled) return;
304
+ const onKeyDown = buildKeyHandler(params);
305
+ const onMouseDown = (event) => {
306
+ const node = containerRef.current;
307
+ const target = event.target instanceof Node ? event.target : null;
308
+ const clickedOutside = node !== null && (target === null || !node.contains(target));
309
+ if (clickedOutside) onClose();
310
+ };
311
+ document.addEventListener("keydown", onKeyDown);
312
+ document.addEventListener("mousedown", onMouseDown);
313
+ return () => {
314
+ document.removeEventListener("keydown", onKeyDown);
315
+ document.removeEventListener("mousedown", onMouseDown);
316
+ };
317
+ }, [containerRef, enabled, onClose, params]);
318
+ }
319
+ var BORDER_RADIUS = 8;
320
+ var BORDER_WIDTH = 1;
321
+ var MENU_PADDING = 4;
322
+ var MENU_MAX_HEIGHT = 300;
323
+ var MENU_TOP_GAP = 4;
324
+ var MENU_Z_INDEX = 1e3;
325
+ var MENU_ELEVATION = 8;
326
+ var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
327
+ var styles6 = StyleSheet.create({
328
+ popover: {
329
+ position: "absolute",
330
+ top: "100%",
331
+ left: 0,
332
+ right: 0,
333
+ marginTop: MENU_TOP_GAP,
334
+ zIndex: MENU_Z_INDEX,
335
+ borderWidth: BORDER_WIDTH,
336
+ borderRadius: BORDER_RADIUS,
337
+ padding: MENU_PADDING,
338
+ maxHeight: MENU_MAX_HEIGHT,
339
+ boxShadow: MENU_BOX_SHADOW,
340
+ elevation: MENU_ELEVATION
341
+ }
342
+ });
343
+ var InlineMenu = ({
344
+ testID,
345
+ accessibilityLabel,
346
+ value,
347
+ options,
348
+ containerRef,
349
+ onSelect,
350
+ onClose
351
+ }) => {
352
+ const { theme } = useUi();
353
+ const { colors } = theme;
354
+ const selectedIndex = useMemo(
355
+ () => Math.max(0, options.findIndex((opt) => opt.value === value)),
356
+ [options, value]
357
+ );
358
+ const [highlightedIndex, setHighlightedIndex] = useState(() => selectedIndex);
359
+ const selectAt = useCallback(
360
+ (index) => {
361
+ const option = options[index];
362
+ if (option !== void 0) onSelect(option.value);
363
+ },
364
+ [options, onSelect]
365
+ );
366
+ const selectHighlighted = useCallback(
367
+ () => {
368
+ selectAt(highlightedIndex);
369
+ },
370
+ [selectAt, highlightedIndex]
371
+ );
372
+ useMenuKeyboard({
373
+ containerRef,
374
+ enabled: true,
375
+ itemCount: options.length,
376
+ onHighlightChange: setHighlightedIndex,
377
+ onSelectHighlighted: selectHighlighted,
378
+ onClose
379
+ });
380
+ const popoverStyle = useMemo(
381
+ () => [styles6.popover, { borderColor: colors.border, backgroundColor: colors.surface }],
382
+ [colors.border, colors.surface]
383
+ );
384
+ return /* @__PURE__ */ jsx(
385
+ View,
386
+ {
387
+ accessibilityRole: "menu",
388
+ "aria-label": accessibilityLabel,
389
+ style: popoverStyle,
390
+ testID: `${testID}-menu`,
391
+ children: /* @__PURE__ */ jsx(ScrollView, { keyboardShouldPersistTaps: "handled", children: options.map((option, index) => /* @__PURE__ */ jsx(
392
+ OptionRow,
393
+ {
394
+ isHighlighted: index === highlightedIndex,
395
+ isSelected: option.value === value,
396
+ label: option.label,
397
+ testID: `${testID}-option-${String(option.value)}`,
398
+ onSelect: () => onSelect(option.value)
399
+ },
400
+ String(option.value)
401
+ )) })
402
+ }
403
+ );
404
+ };
405
+ var MENU_BREAKPOINT = 768;
406
+ var WEB_PLATFORM = "web";
407
+ function resolveDropdownVariant(explicit, width, platformOS) {
408
+ if (explicit !== void 0) return explicit;
409
+ const isWideWeb = platformOS === WEB_PLATFORM && width >= MENU_BREAKPOINT;
410
+ return isWideWeb ? "menu" /* Menu */ : "modal" /* Modal */;
411
+ }
412
+ function useResolvedDropdownVariant(explicit) {
413
+ const { width } = useWindowDimensions();
414
+ return resolveDropdownVariant(explicit, width, Platform.OS);
415
+ }
416
+ var BORDER_RADIUS2 = 8;
417
+ var BORDER_WIDTH2 = 1;
418
+ var BODY_FONT_SIZE2 = 14;
215
419
  var MODAL_PADDING = 8;
216
420
  var MODAL_MIN_WIDTH = 200;
217
421
  var MODAL_MAX_HEIGHT = 300;
218
422
  var CONTAINER_PADDING_H = 12;
219
423
  var CONTAINER_PADDING_V = 10;
220
- var styles5 = StyleSheet.create({
424
+ var styles7 = StyleSheet.create({
425
+ anchor: { position: "relative" },
221
426
  container: {
222
- borderWidth: BORDER_WIDTH,
223
- borderRadius: BORDER_RADIUS,
427
+ borderWidth: BORDER_WIDTH2,
428
+ borderRadius: BORDER_RADIUS2,
224
429
  paddingHorizontal: CONTAINER_PADDING_H,
225
430
  paddingVertical: CONTAINER_PADDING_V
226
431
  },
227
- selectedText: { fontSize: BODY_FONT_SIZE },
432
+ selectedText: { fontSize: BODY_FONT_SIZE2 },
228
433
  modalOverlay: {
229
434
  flex: 1,
230
435
  justifyContent: "center",
@@ -232,18 +437,11 @@ var styles5 = StyleSheet.create({
232
437
  backgroundColor: MODAL_OVERLAY_COLOR
233
438
  },
234
439
  modalContent: {
235
- borderRadius: BORDER_RADIUS,
440
+ borderRadius: BORDER_RADIUS2,
236
441
  padding: MODAL_PADDING,
237
442
  minWidth: MODAL_MIN_WIDTH,
238
443
  maxHeight: MODAL_MAX_HEIGHT
239
- },
240
- option: {
241
- paddingHorizontal: 16,
242
- paddingVertical: 12,
243
- borderRadius: OPTION_BORDER_RADIUS
244
- },
245
- optionText: { fontSize: BODY_FONT_SIZE },
246
- selectedOption: { fontWeight: SELECTED_FONT_WEIGHT }
444
+ }
247
445
  });
248
446
  var ModalDropdown = ({
249
447
  testID,
@@ -251,19 +449,23 @@ var ModalDropdown = ({
251
449
  accessibilityHint,
252
450
  value,
253
451
  options,
254
- onChange
452
+ onChange,
453
+ variant
255
454
  }) => {
256
455
  const { theme, t } = useUi();
257
456
  const { colors } = theme;
457
+ const resolvedVariant = useResolvedDropdownVariant(variant);
458
+ const isMenu = resolvedVariant === "menu" /* Menu */;
258
459
  const [isOpen, setIsOpen] = useState(false);
460
+ const anchorRef = useRef(null);
259
461
  const dialogRef = useRef(null);
260
- useFocusTrap(dialogRef, isOpen);
462
+ useFocusTrap(dialogRef, isOpen && !isMenu);
261
463
  const selectedLabel = useMemo(() => {
262
464
  const found = options.find((opt) => opt.value === value);
263
465
  return found?.label ?? t("common.selectPlaceholder");
264
466
  }, [options, value, t]);
265
- const handleOpen = useCallback(() => {
266
- setIsOpen(true);
467
+ const handleToggle = useCallback(() => {
468
+ setIsOpen((prev) => !prev);
267
469
  }, []);
268
470
  const handleClose = useCallback(() => {
269
471
  setIsOpen(false);
@@ -276,44 +478,27 @@ var ModalDropdown = ({
276
478
  [onChange]
277
479
  );
278
480
  const containerStyle = useMemo(
279
- () => [styles5.container, { borderColor: colors.border, backgroundColor: colors.surface }],
481
+ () => [styles7.container, { borderColor: colors.border, backgroundColor: colors.surface }],
280
482
  [colors.border, colors.surface]
281
483
  );
282
484
  const modalContentStyle = useMemo(
283
- () => [styles5.modalContent, { backgroundColor: colors.surface }],
485
+ () => [styles7.modalContent, { backgroundColor: colors.surface }],
284
486
  [colors.surface]
285
487
  );
286
- const renderOption = useCallback(
287
- ({ item }) => {
288
- const isSelected = item.value === value;
289
- return /* @__PURE__ */ jsx(
290
- TouchableOpacity,
291
- {
292
- accessible: true,
293
- accessibilityHint: t("common.selectOptionHint"),
294
- accessibilityLabel: item.label,
295
- accessibilityRole: "button",
296
- style: [styles5.option, isSelected ? { backgroundColor: colors.surfaceElevated } : null],
297
- testID: `${testID}-option-${String(item.value)}`,
298
- onPress: () => handleSelect(item.value),
299
- children: /* @__PURE__ */ jsx(
300
- Text,
301
- {
302
- style: [
303
- styles5.optionText,
304
- { color: colors.text },
305
- isSelected ? styles5.selectedOption : null
306
- ],
307
- children: item.label
308
- }
309
- )
310
- }
311
- );
312
- },
313
- [colors.surfaceElevated, colors.text, handleSelect, testID, value, t]
488
+ const renderModalOption = useCallback(
489
+ ({ item }) => /* @__PURE__ */ jsx(
490
+ OptionRow,
491
+ {
492
+ isSelected: item.value === value,
493
+ label: item.label,
494
+ testID: `${testID}-option-${String(item.value)}`,
495
+ onSelect: () => handleSelect(item.value)
496
+ }
497
+ ),
498
+ [handleSelect, testID, value]
314
499
  );
315
500
  const keyExtractor = useCallback((item) => String(item.value), []);
316
- return /* @__PURE__ */ jsxs(Fragment, { children: [
501
+ return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: styles7.anchor, children: [
317
502
  /* @__PURE__ */ jsx(
318
503
  TouchableOpacity,
319
504
  {
@@ -321,13 +506,26 @@ var ModalDropdown = ({
321
506
  accessibilityHint,
322
507
  accessibilityLabel,
323
508
  accessibilityRole: "button",
509
+ "aria-expanded": isOpen,
324
510
  style: containerStyle,
325
511
  testID,
326
- onPress: handleOpen,
327
- children: /* @__PURE__ */ jsx(Text, { style: [styles5.selectedText, { color: colors.text }], children: selectedLabel })
512
+ onPress: handleToggle,
513
+ children: /* @__PURE__ */ jsx(Text, { style: [styles7.selectedText, { color: colors.text }], children: selectedLabel })
328
514
  }
329
515
  ),
330
- /* @__PURE__ */ jsx(Modal, { transparent: true, animationType: "fade", visible: isOpen, onRequestClose: handleClose, children: /* @__PURE__ */ jsx(
516
+ isMenu && isOpen ? /* @__PURE__ */ jsx(
517
+ InlineMenu,
518
+ {
519
+ accessibilityLabel,
520
+ containerRef: anchorRef,
521
+ options,
522
+ testID,
523
+ value,
524
+ onClose: handleClose,
525
+ onSelect: handleSelect
526
+ }
527
+ ) : null,
528
+ !isMenu ? /* @__PURE__ */ jsx(Modal, { transparent: true, animationType: "fade", visible: isOpen, onRequestClose: handleClose, children: /* @__PURE__ */ jsx(
331
529
  TouchableOpacity,
332
530
  {
333
531
  accessible: true,
@@ -335,7 +533,7 @@ var ModalDropdown = ({
335
533
  accessibilityLabel: t("common.dismissDropdown"),
336
534
  accessibilityRole: "button",
337
535
  activeOpacity: 1,
338
- style: styles5.modalOverlay,
536
+ style: styles7.modalOverlay,
339
537
  testID: `${testID}-backdrop`,
340
538
  onPress: handleClose,
341
539
  children: /* @__PURE__ */ jsx(
@@ -346,21 +544,82 @@ var ModalDropdown = ({
346
544
  "aria-label": accessibilityLabel,
347
545
  role: "dialog",
348
546
  style: modalContentStyle,
349
- children: /* @__PURE__ */ jsx(
350
- FlatList,
351
- {
352
- data: [...options],
353
- keyExtractor,
354
- renderItem: renderOption
355
- }
356
- )
547
+ children: /* @__PURE__ */ jsx(FlatList, { data: [...options], keyExtractor, renderItem: renderModalOption })
357
548
  }
358
549
  )
359
550
  }
360
- ) })
551
+ ) }) : null
361
552
  ] });
362
553
  };
554
+ var CLOSE_ICON_SIZE = 18;
555
+ var styles8 = StyleSheet.create({
556
+ container: {
557
+ flex: 1,
558
+ padding: 16
559
+ },
560
+ headerRow: {
561
+ flexDirection: "row",
562
+ alignItems: "center",
563
+ justifyContent: "space-between"
564
+ },
565
+ closeButton: {
566
+ padding: 6,
567
+ borderRadius: 6
568
+ }
569
+ });
570
+ var ModalShell = ({
571
+ visible,
572
+ onCancel,
573
+ title,
574
+ children,
575
+ contentStyle,
576
+ showClose = true
577
+ }) => {
578
+ const { theme, t } = useUi();
579
+ const containerStyle = React.useMemo(
580
+ () => [styles8.container, { backgroundColor: theme.colors.background }, contentStyle],
581
+ [theme.colors.background, contentStyle]
582
+ );
583
+ const closeA11yLabel = t("quizTemplates.cancel");
584
+ const accessibleTitle = typeof title === "string" ? title : t("common.dialog");
585
+ return /* @__PURE__ */ jsx(
586
+ Modal,
587
+ {
588
+ animationType: "slide",
589
+ testID: LAYOUT_TEST_IDS.modalShell,
590
+ visible,
591
+ onRequestClose: onCancel,
592
+ children: /* @__PURE__ */ jsxs(
593
+ ScrollView,
594
+ {
595
+ accessibilityViewIsModal: true,
596
+ "aria-label": accessibleTitle,
597
+ role: "dialog",
598
+ style: containerStyle,
599
+ children: [
600
+ /* @__PURE__ */ jsxs(View, { style: styles8.headerRow, children: [
601
+ /* @__PURE__ */ jsx(Heading, { children: title ?? t("close") }),
602
+ showClose ? /* @__PURE__ */ jsx(
603
+ TouchableOpacity,
604
+ {
605
+ accessibilityHint: t("common.closeDialogHint"),
606
+ accessibilityLabel: closeA11yLabel,
607
+ accessibilityRole: "button",
608
+ style: styles8.closeButton,
609
+ testID: LAYOUT_TEST_IDS.modalShellClose,
610
+ onPress: onCancel,
611
+ children: /* @__PURE__ */ jsx(SvgIcon, { color: theme.colors.textSecondary, name: "close", size: CLOSE_ICON_SIZE })
612
+ }
613
+ ) : null
614
+ ] }),
615
+ children
616
+ ]
617
+ }
618
+ )
619
+ }
620
+ );
621
+ };
363
622
 
364
- export { DEFAULT_BILLING_ROUTE, Heading, LAYOUT_TEST_IDS, ModalDropdown, Section, StatusBadge, UpgradePrompt, useFocusTrap };
623
+ export { DEFAULT_BILLING_ROUTE, DropdownVariant, Heading, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, StatusBadge, UpgradePrompt, useFocusTrap };
365
624
  //# sourceMappingURL=index.mjs.map
366
625
  //# sourceMappingURL=index.mjs.map