@stack-spot/portal-layout 0.0.11 → 0.0.13

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.
Files changed (53) hide show
  1. package/dist/Layout.d.ts.map +1 -1
  2. package/dist/Layout.js +2 -1
  3. package/dist/Layout.js.map +1 -1
  4. package/dist/LayoutOverlayManager.d.ts +4 -0
  5. package/dist/LayoutOverlayManager.d.ts.map +1 -1
  6. package/dist/LayoutOverlayManager.js +51 -21
  7. package/dist/LayoutOverlayManager.js.map +1 -1
  8. package/dist/components/OverlayContent.d.ts +1 -0
  9. package/dist/components/OverlayContent.d.ts.map +1 -1
  10. package/dist/components/OverlayContent.js +2 -1
  11. package/dist/components/OverlayContent.js.map +1 -1
  12. package/dist/components/SelectionList.d.ts +2 -1
  13. package/dist/components/SelectionList.d.ts.map +1 -1
  14. package/dist/components/SelectionList.js +91 -30
  15. package/dist/components/SelectionList.js.map +1 -1
  16. package/dist/components/UserMenu.d.ts.map +1 -1
  17. package/dist/components/UserMenu.js +14 -3
  18. package/dist/components/UserMenu.js.map +1 -1
  19. package/dist/components/menu/MenuContent.d.ts.map +1 -1
  20. package/dist/components/menu/MenuContent.js +11 -7
  21. package/dist/components/menu/MenuContent.js.map +1 -1
  22. package/dist/components/menu/MenuSections.d.ts.map +1 -1
  23. package/dist/components/menu/MenuSections.js +12 -2
  24. package/dist/components/menu/MenuSections.js.map +1 -1
  25. package/dist/components/menu/PageSelector.d.ts.map +1 -1
  26. package/dist/components/menu/PageSelector.js +13 -2
  27. package/dist/components/menu/PageSelector.js.map +1 -1
  28. package/dist/elements.d.ts +18 -0
  29. package/dist/elements.d.ts.map +1 -0
  30. package/dist/elements.js +18 -0
  31. package/dist/elements.js.map +1 -0
  32. package/dist/index.d.ts +2 -1
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +2 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/layout.css +13 -1
  37. package/dist/utils.d.ts +51 -0
  38. package/dist/utils.d.ts.map +1 -1
  39. package/dist/utils.js +93 -0
  40. package/dist/utils.js.map +1 -1
  41. package/package.json +1 -1
  42. package/src/Layout.tsx +14 -11
  43. package/src/LayoutOverlayManager.tsx +54 -30
  44. package/src/components/OverlayContent.tsx +3 -1
  45. package/src/components/SelectionList.tsx +115 -28
  46. package/src/components/UserMenu.tsx +23 -3
  47. package/src/components/menu/MenuContent.tsx +19 -7
  48. package/src/components/menu/MenuSections.tsx +16 -1
  49. package/src/components/menu/PageSelector.tsx +24 -2
  50. package/src/elements.ts +24 -0
  51. package/src/index.ts +2 -1
  52. package/src/layout.css +13 -1
  53. package/src/utils.ts +107 -0
@@ -2,7 +2,8 @@ import { IconBox, Text } from '@citric/core'
2
2
  import { ArrowRight, Select } from '@citric/icons'
3
3
  import { LoadingCircular } from '@citric/ui'
4
4
  import { theme } from '@stack-spot/portal-theme'
5
- import { useMemo, useState } from 'react'
5
+ import { Dictionary, interpolate, useTranslate } from '@stack-spot/portal-translate'
6
+ import { useMemo, useRef, useState } from 'react'
6
7
  import { styled } from 'styled-components'
7
8
  import { ListAction, SelectionList } from '../SelectionList'
8
9
  import { MENU_CONTENT_PADDING as PADDING } from './constants'
@@ -76,7 +77,9 @@ const SelectorBox = styled.div`
76
77
  `
77
78
 
78
79
  export const PageSelector = ({ options, value, button, loading, title }: Selector) => {
80
+ const t = useTranslate(dictionary)
79
81
  const [visible, setVisible] = useState(false)
82
+ const id = useRef(`pageSelector${title || Math.random()}`)
80
83
  const { optionsWithIcon, selected } = useMemo(
81
84
  () => {
82
85
  let selected = options[0]
@@ -100,13 +103,22 @@ export const PageSelector = ({ options, value, button, loading, title }: Selecto
100
103
  : (
101
104
  <>
102
105
  {title && <Text colorScheme="light.700" sx={{ mb: 3 }}>{title}</Text>}
103
- <a onClick={() => setVisible(true)} aria-label={value}>
106
+ <a
107
+ onClick={() => setVisible(true)}
108
+ onKeyDown={e => e.key === 'Enter' && setVisible(true)}
109
+ title={value}
110
+ tabIndex={0}
111
+ aria-label={interpolate(t.accessibility, [value])}
112
+ aria-expanded={visible}
113
+ aria-controls={id.current}
114
+ >
104
115
  {selected?.icon && <IconBox>{selected?.icon}</IconBox>}
105
116
  <Text appearance="body2" className="label">{selected?.label ?? button?.label ?? value}</Text>
106
117
  <IconBox size="xs"><Select /></IconBox>
107
118
  </a>
108
119
 
109
120
  <SelectionList
121
+ id={id.current}
110
122
  visible={visible}
111
123
  items={optionsWithIcon}
112
124
  onHide={() => setVisible(false)}
@@ -119,3 +131,13 @@ export const PageSelector = ({ options, value, button, loading, title }: Selecto
119
131
  </SelectorBox>
120
132
  )
121
133
  }
134
+
135
+ const dictionary = {
136
+ en: {
137
+ accessibility: 'Current value: $0. Press Enter to change.',
138
+ },
139
+ pt: {
140
+ accessibility: 'Valor atual: $0. Aperte Enter para mudar.',
141
+ },
142
+ } satisfies Dictionary
143
+
@@ -0,0 +1,24 @@
1
+ export const elementIds = {
2
+ layout: 'layout',
3
+ backdrop: 'backdrop',
4
+ modal: 'modal',
5
+ rightPanel: 'rightPanel',
6
+ bottomDialog: 'bottomDialog',
7
+ page: 'page',
8
+ content: 'content',
9
+ header: 'header',
10
+ menu: 'menu',
11
+ menuContent: 'menuContent',
12
+ menuSections: 'menuSections',
13
+ accessibilityAnnouncer: 'accessibilityAnnouncer',
14
+ } as const
15
+
16
+ export type LayoutElement = keyof typeof elementIds
17
+ export type LayoutElements = Record<LayoutElement, HTMLElement | null>
18
+
19
+ export function getLayoutElements() {
20
+ return (Object.keys(elementIds) as LayoutElement[]).reduce<LayoutElements>(
21
+ (result, id) => ({ ...result, [id]: document.getElementById(id) }),
22
+ {} as LayoutElements,
23
+ )
24
+ }
package/src/index.ts CHANGED
@@ -8,7 +8,8 @@ export { MenuContent } from './components/menu/MenuContent'
8
8
  export { MenuSections } from './components/menu/MenuSections'
9
9
  export * from './components/menu/types'
10
10
  export * from './components/types'
11
+ export * from './elements'
11
12
  export * from './errors'
12
13
  export { Logo as StackspotLogo } from './svg/Logo'
13
- export { valueOfLayoutVar } from './utils'
14
+ export * from './utils'
14
15
 
package/src/layout.css CHANGED
@@ -188,7 +188,9 @@ body {
188
188
  }
189
189
 
190
190
  #menuSections .toggle:hover,
191
- #menuSections > ul li a:hover {
191
+ #menuSections > ul li a:hover,
192
+ #menuSections .toggle:focus,
193
+ #menuSections > ul li a:focus {
192
194
  background: var(--light-500);
193
195
  }
194
196
 
@@ -313,6 +315,8 @@ body {
313
315
  transition: right var(--right-panel-animation-duration);
314
316
  background-color: var(--light-400);
315
317
  right: -800px;
318
+ border-top-left-radius: 1rem;
319
+ border-bottom-left-radius: 1rem;
316
320
  }
317
321
 
318
322
  #rightPanel.small {
@@ -417,3 +421,11 @@ ul {
417
421
  i {
418
422
  fill: var(--light-contrastText);
419
423
  }
424
+
425
+ /* Accessibility */
426
+ #accessibilityAnnouncer {
427
+ position: fixed;
428
+ width: 0;
429
+ height: 0;
430
+ overflow: hidden;
431
+ }
package/src/utils.ts CHANGED
@@ -1,7 +1,114 @@
1
1
  import { valueOf } from '@stack-spot/portal-theme'
2
+ import { getLayoutElements } from './elements'
2
3
 
3
4
  export function valueOfLayoutVar(varname: string): string {
4
5
  const layout = document.getElementById('layout')
5
6
  if (!layout) return ''
6
7
  return valueOf(varname, layout)
7
8
  }
9
+
10
+ /**
11
+ * Important for accessibility.
12
+ *
13
+ * Makes it so we focus the next focusable element in the DOM hierarchy, disregarding the element passed as parameter and its children.
14
+ *
15
+ * If there's no next focusable element, the first focusable of the page will be focused. If the page doesn't contain any focusable
16
+ * element, nothing happens.
17
+ *
18
+ * @param current the reference element to focus the next. If not provided, will be the currently active element.
19
+ */
20
+ export function focusNextIgnoringChildren(current?: HTMLElement | null) {
21
+ current = current ?? document.activeElement as HTMLElement
22
+ while (current && !current.nextElementSibling) {
23
+ current = current?.parentElement
24
+ }
25
+ current = current?.nextElementSibling as HTMLElement
26
+ while (current && current.tabIndex < 0) {
27
+ current = (current.children.length ? current.firstChild : current.nextElementSibling) as HTMLElement
28
+ }
29
+ if (current) current?.focus?.()
30
+ else focusFirstChild(document)
31
+ }
32
+
33
+ type TagPriority = 'a' | 'button' | 'input' | 'textarea' | 'select' | 'other'
34
+ type TagPriorityElement = TagPriority | TagPriority[]
35
+
36
+ interface FocusOptions {
37
+ /**
38
+ * Instead of focusing the first element overall, focus the first according to this list of priorities.
39
+ */
40
+ priority?: TagPriorityElement[],
41
+ /**
42
+ * Ignores any element that matches this query selector.
43
+ */
44
+ ignore?: string,
45
+ }
46
+
47
+ const selectors: Record<TagPriority, string> = {
48
+ a: 'a[href]:not(:disabled)',
49
+ button: 'button:not(:disabled)',
50
+ input: 'input:not(:disabled):not([type="hidden"])',
51
+ select: 'textarea:not(:disabled)',
52
+ textarea: 'select:not(:disabled)',
53
+ other: '[tabindex]:not([tabindex="-1"])',
54
+ }
55
+
56
+ /**
57
+ * Focus the first focusable child of the element provided. If the element has no focusable child, nothing happens.
58
+ *
59
+ * A priority list can be passed in the second parameter, as an option. If it's provided, it will focus the first element according to the
60
+ * list.
61
+ *
62
+ * An ignore query selector can also be passed in the options parameter. If the first focusable element matches the query selector, the
63
+ * next element is focused instead.
64
+ *
65
+ * @example
66
+ * Suppose the children of element are: h1, button, p, input, select.
67
+ * 1. We don't pass a priority list. The focused element will be the button.
68
+ * 2. Our priority list is ['button']. The focused element will be the button.
69
+ * 3. Our priority list is ['input', 'button']. The focused element will be the input.
70
+ * 4. Our priority list is ['select', 'input']. The focused element will be the select.
71
+ * 5. Our priority list is [['select', 'input'], 'button']. The focused element will be the input.
72
+ *
73
+ * @param element the element to search a child to focus.
74
+ * @param options optional.
75
+ */
76
+ export function focusFirstChild(element: HTMLElement | Document | null | undefined, { priority = [], ignore }: FocusOptions = {}) {
77
+ let focusable: NodeListOf<HTMLElement> | null | undefined
78
+ let missing: TagPriority[] = ['a', 'button', 'input', 'other', 'select', 'textarea']
79
+ for (const p of priority) {
80
+ const tags = Array.isArray(p) ? p : [p]
81
+ const querySelectors = tags.map(t => {
82
+ missing = missing.filter(tag => tag != t)
83
+ return selectors[t]
84
+ })
85
+ focusable = element?.querySelectorAll(querySelectors.join(', '))
86
+ if (focusable) break
87
+ }
88
+ if (!focusable) {
89
+ element?.querySelectorAll(missing.map(t => selectors[t]).join(', '))
90
+ }
91
+ let elementToFocus: HTMLElement | undefined
92
+ for (const f of focusable ?? []) {
93
+ if (!ignore || !f.matches(ignore)) {
94
+ const styles = window.getComputedStyle(f)
95
+ if (styles.display != 'none' && styles.visibility != 'hidden') {
96
+ elementToFocus = f
97
+ break
98
+ }
99
+ }
100
+ }
101
+ elementToFocus?.focus?.()
102
+ }
103
+
104
+ /**
105
+ * Accessibility: makes the screen reader announce some text out loud.
106
+ *
107
+ * @param text the message for the screen reader to read.
108
+ */
109
+ export function announce(text: string) {
110
+ const { accessibilityAnnouncer } = getLayoutElements()
111
+ if (!accessibilityAnnouncer) return
112
+ accessibilityAnnouncer.textContent = text
113
+ setTimeout(() => accessibilityAnnouncer.textContent = '', 1000)
114
+ }