@instructure/ui-global-nav 0.0.0-stage → 11.7.10
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/CHANGELOG.md +0 -0
- package/LICENSE.md +28 -0
- package/README.md +25 -2
- package/es/GlobalNav/v1/CollapseButton/index.js +80 -0
- package/es/GlobalNav/v1/CollapseButton/props.js +1 -0
- package/es/GlobalNav/v1/CollapseButton/styles.js +64 -0
- package/es/GlobalNav/v1/CollapsedSubNav/index.js +59 -0
- package/es/GlobalNav/v1/CollapsedSubNav/props.js +1 -0
- package/es/GlobalNav/v1/CollapsedSubNav/styles.js +82 -0
- package/es/GlobalNav/v1/InstitutionLogo/index.js +93 -0
- package/es/GlobalNav/v1/InstitutionLogo/props.js +1 -0
- package/es/GlobalNav/v1/InstitutionLogo/styles.js +69 -0
- package/es/GlobalNav/v1/MainNavItem/index.js +129 -0
- package/es/GlobalNav/v1/MainNavItem/props.js +1 -0
- package/es/GlobalNav/v1/MainNavItem/styles.js +117 -0
- package/es/GlobalNav/v1/SubNav/index.js +121 -0
- package/es/GlobalNav/v1/SubNav/props.js +1 -0
- package/es/GlobalNav/v1/SubNav/styles.js +52 -0
- package/es/GlobalNav/v1/SubNavItem/index.js +78 -0
- package/es/GlobalNav/v1/SubNavItem/props.js +1 -0
- package/es/GlobalNav/v1/SubNavItem/styles.js +94 -0
- package/es/GlobalNav/v1/SubNavLevel/index.js +160 -0
- package/es/GlobalNav/v1/SubNavLevel/props.js +1 -0
- package/es/GlobalNav/v1/SubNavLevel/styles.js +94 -0
- package/es/GlobalNav/v1/index.js +222 -0
- package/es/GlobalNav/v1/props.js +28 -0
- package/es/GlobalNav/v1/styles.js +110 -0
- package/es/GlobalNav/v1/utils/getItemElementProps.js +64 -0
- package/es/exports/a.js +24 -0
- package/lib/GlobalNav/v1/CollapseButton/index.js +85 -0
- package/lib/GlobalNav/v1/CollapseButton/props.js +5 -0
- package/lib/GlobalNav/v1/CollapseButton/styles.js +70 -0
- package/lib/GlobalNav/v1/CollapsedSubNav/index.js +64 -0
- package/lib/GlobalNav/v1/CollapsedSubNav/props.js +5 -0
- package/lib/GlobalNav/v1/CollapsedSubNav/styles.js +88 -0
- package/lib/GlobalNav/v1/InstitutionLogo/index.js +98 -0
- package/lib/GlobalNav/v1/InstitutionLogo/props.js +5 -0
- package/lib/GlobalNav/v1/InstitutionLogo/styles.js +75 -0
- package/lib/GlobalNav/v1/MainNavItem/index.js +134 -0
- package/lib/GlobalNav/v1/MainNavItem/props.js +5 -0
- package/lib/GlobalNav/v1/MainNavItem/styles.js +123 -0
- package/lib/GlobalNav/v1/SubNav/index.js +128 -0
- package/lib/GlobalNav/v1/SubNav/props.js +5 -0
- package/lib/GlobalNav/v1/SubNav/styles.js +58 -0
- package/lib/GlobalNav/v1/SubNavItem/index.js +83 -0
- package/lib/GlobalNav/v1/SubNavItem/props.js +5 -0
- package/lib/GlobalNav/v1/SubNavItem/styles.js +100 -0
- package/lib/GlobalNav/v1/SubNavLevel/index.js +165 -0
- package/lib/GlobalNav/v1/SubNavLevel/props.js +5 -0
- package/lib/GlobalNav/v1/SubNavLevel/styles.js +100 -0
- package/lib/GlobalNav/v1/index.js +228 -0
- package/lib/GlobalNav/v1/props.js +33 -0
- package/lib/GlobalNav/v1/styles.js +116 -0
- package/lib/GlobalNav/v1/utils/getItemElementProps.js +70 -0
- package/lib/exports/a.js +12 -0
- package/lib/package.json +1 -0
- package/package.json +77 -3
- package/src/GlobalNav/v1/CollapseButton/index.tsx +87 -0
- package/src/GlobalNav/v1/CollapseButton/props.ts +64 -0
- package/src/GlobalNav/v1/CollapseButton/styles.ts +76 -0
- package/src/GlobalNav/v1/CollapsedSubNav/index.tsx +60 -0
- package/src/GlobalNav/v1/CollapsedSubNav/props.ts +42 -0
- package/src/GlobalNav/v1/CollapsedSubNav/styles.ts +92 -0
- package/src/GlobalNav/v1/InstitutionLogo/index.tsx +86 -0
- package/src/GlobalNav/v1/InstitutionLogo/props.ts +42 -0
- package/src/GlobalNav/v1/InstitutionLogo/styles.ts +82 -0
- package/src/GlobalNav/v1/MainNavItem/index.tsx +132 -0
- package/src/GlobalNav/v1/MainNavItem/props.ts +65 -0
- package/src/GlobalNav/v1/MainNavItem/styles.ts +137 -0
- package/src/GlobalNav/v1/README.md +234 -0
- package/src/GlobalNav/v1/SubNav/index.tsx +132 -0
- package/src/GlobalNav/v1/SubNav/props.ts +81 -0
- package/src/GlobalNav/v1/SubNav/styles.ts +58 -0
- package/src/GlobalNav/v1/SubNavItem/index.tsx +79 -0
- package/src/GlobalNav/v1/SubNavItem/props.ts +48 -0
- package/src/GlobalNav/v1/SubNavItem/styles.ts +110 -0
- package/src/GlobalNav/v1/SubNavLevel/index.tsx +193 -0
- package/src/GlobalNav/v1/SubNavLevel/props.ts +75 -0
- package/src/GlobalNav/v1/SubNavLevel/styles.ts +103 -0
- package/src/GlobalNav/v1/index.tsx +256 -0
- package/src/GlobalNav/v1/props.ts +328 -0
- package/src/GlobalNav/v1/styles.ts +132 -0
- package/src/GlobalNav/v1/utils/getItemElementProps.ts +80 -0
- package/src/exports/a.ts +35 -0
- package/tsconfig.build.json +53 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +4 -0
- package/types/GlobalNav/v1/CollapseButton/index.d.ts +11 -0
- package/types/GlobalNav/v1/CollapseButton/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/CollapseButton/props.d.ts +31 -0
- package/types/GlobalNav/v1/CollapseButton/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/CollapseButton/styles.d.ts +22 -0
- package/types/GlobalNav/v1/CollapseButton/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/CollapsedSubNav/index.d.ts +11 -0
- package/types/GlobalNav/v1/CollapsedSubNav/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/CollapsedSubNav/props.d.ts +15 -0
- package/types/GlobalNav/v1/CollapsedSubNav/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/CollapsedSubNav/styles.d.ts +14 -0
- package/types/GlobalNav/v1/CollapsedSubNav/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/InstitutionLogo/index.d.ts +11 -0
- package/types/GlobalNav/v1/InstitutionLogo/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/InstitutionLogo/props.d.ts +15 -0
- package/types/GlobalNav/v1/InstitutionLogo/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/InstitutionLogo/styles.d.ts +20 -0
- package/types/GlobalNav/v1/InstitutionLogo/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/MainNavItem/index.d.ts +11 -0
- package/types/GlobalNav/v1/MainNavItem/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/MainNavItem/props.d.ts +32 -0
- package/types/GlobalNav/v1/MainNavItem/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/MainNavItem/styles.d.ts +19 -0
- package/types/GlobalNav/v1/MainNavItem/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNav/index.d.ts +11 -0
- package/types/GlobalNav/v1/SubNav/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNav/props.d.ts +52 -0
- package/types/GlobalNav/v1/SubNav/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNav/styles.d.ts +13 -0
- package/types/GlobalNav/v1/SubNav/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNavItem/index.d.ts +11 -0
- package/types/GlobalNav/v1/SubNavItem/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNavItem/props.d.ts +20 -0
- package/types/GlobalNav/v1/SubNavItem/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNavItem/styles.d.ts +18 -0
- package/types/GlobalNav/v1/SubNavItem/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNavLevel/index.d.ts +11 -0
- package/types/GlobalNav/v1/SubNavLevel/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNavLevel/props.d.ts +45 -0
- package/types/GlobalNav/v1/SubNavLevel/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/SubNavLevel/styles.d.ts +14 -0
- package/types/GlobalNav/v1/SubNavLevel/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/index.d.ts +59 -0
- package/types/GlobalNav/v1/index.d.ts.map +1 -0
- package/types/GlobalNav/v1/props.d.ts +247 -0
- package/types/GlobalNav/v1/props.d.ts.map +1 -0
- package/types/GlobalNav/v1/styles.d.ts +23 -0
- package/types/GlobalNav/v1/styles.d.ts.map +1 -0
- package/types/GlobalNav/v1/utils/getItemElementProps.d.ts +37 -0
- package/types/GlobalNav/v1/utils/getItemElementProps.d.ts.map +1 -0
- package/types/exports/a.d.ts +3 -0
- package/types/exports/a.d.ts.map +1 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { forwardRef, useEffect, useLayoutEffect, useRef } from 'react'
|
|
26
|
+
import type { MouseEvent, SyntheticEvent } from 'react'
|
|
27
|
+
import { useStyleNew } from '@instructure/emotion'
|
|
28
|
+
import { omitProps, useDeterministicId } from '@instructure/ui-react-utils'
|
|
29
|
+
|
|
30
|
+
import type { GlobalNavMainNavItem, GlobalNavProps } from './props.js'
|
|
31
|
+
import { allowedProps } from './props.js'
|
|
32
|
+
import generateStyle, { generateMainNavStyle } from './styles.js'
|
|
33
|
+
import { CollapseButton } from './CollapseButton/index.js'
|
|
34
|
+
import { CollapsedSubNav } from './CollapsedSubNav/index.js'
|
|
35
|
+
import { InstitutionLogo } from './InstitutionLogo/index.js'
|
|
36
|
+
import { MainNavItem } from './MainNavItem/index.js'
|
|
37
|
+
import { SubNav } from './SubNav/index.js'
|
|
38
|
+
import type { FocusIntent } from './SubNav/props.js'
|
|
39
|
+
|
|
40
|
+
// React 18 warns about useLayoutEffect on the server, where there is no layout to measure
|
|
41
|
+
const useClientLayoutEffect =
|
|
42
|
+
typeof window === 'undefined' ? useEffect : useLayoutEffect
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
---
|
|
46
|
+
category: components
|
|
47
|
+
isWIP: true
|
|
48
|
+
---
|
|
49
|
+
**/
|
|
50
|
+
const GlobalNav = forwardRef<HTMLElement, GlobalNavProps>(
|
|
51
|
+
(props: GlobalNavProps, ref) => {
|
|
52
|
+
const {
|
|
53
|
+
label,
|
|
54
|
+
collapsed,
|
|
55
|
+
onCollapse,
|
|
56
|
+
collapseLabel,
|
|
57
|
+
expandLabel,
|
|
58
|
+
backLabel,
|
|
59
|
+
closeSubNavLabel,
|
|
60
|
+
collapseSubNavLabel,
|
|
61
|
+
expandSubNavLabel,
|
|
62
|
+
openSubNav,
|
|
63
|
+
onSubNavChange,
|
|
64
|
+
items,
|
|
65
|
+
logo,
|
|
66
|
+
themeOverride
|
|
67
|
+
} = props
|
|
68
|
+
|
|
69
|
+
const openItem = items.find(
|
|
70
|
+
(item) => item.itemID === openSubNav && item.subNav
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
const styles = useStyleNew({
|
|
74
|
+
generateStyle,
|
|
75
|
+
themeOverride,
|
|
76
|
+
params: { collapsed, subNavOpen: !!openItem },
|
|
77
|
+
componentId: 'GlobalNav',
|
|
78
|
+
displayName: 'GlobalNav'
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// MainNav is a separate token set, and useStyleNew reads one set per call
|
|
82
|
+
const mainNavStyles = useStyleNew({
|
|
83
|
+
generateStyle: generateMainNavStyle,
|
|
84
|
+
params: { collapsed },
|
|
85
|
+
componentId: 'MainNav'
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const getId = useDeterministicId('GlobalNav')
|
|
89
|
+
const subNavId = getId('GlobalNav-subNav')
|
|
90
|
+
const titleId = getId('GlobalNav-subNavTitle')
|
|
91
|
+
|
|
92
|
+
const focusIntentRef = useRef<FocusIntent>()
|
|
93
|
+
const triggerRefs = useRef(new Map<string, HTMLElement>())
|
|
94
|
+
const renderedSubNavRef = useRef<string>()
|
|
95
|
+
const railRef = useRef<HTMLDivElement>(null)
|
|
96
|
+
const headerRef = useRef<HTMLDivElement>(null)
|
|
97
|
+
const collapseButtonRef = useRef<Element | null>(null)
|
|
98
|
+
|
|
99
|
+
// the SubNav buttons line up with the rail's collapse button, whose position depends on the logo
|
|
100
|
+
const updateCollapseButtonTop = () => {
|
|
101
|
+
const rail = railRef.current
|
|
102
|
+
const button = collapseButtonRef.current
|
|
103
|
+
if (!rail || !button) return
|
|
104
|
+
const buttonRect = button.getBoundingClientRect()
|
|
105
|
+
const top =
|
|
106
|
+
buttonRect.top - rail.getBoundingClientRect().top - rail.clientTop
|
|
107
|
+
rail.style.setProperty('--globalNav-collapseButtonTop', `${top}px`)
|
|
108
|
+
rail.style.setProperty(
|
|
109
|
+
'--globalNav-collapseButtonHeight',
|
|
110
|
+
`${buttonRect.height}px`
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// after every render, because the theme and the collapsed state move the button,
|
|
115
|
+
// and before paint, so a SubNav that is open on page load doesn't jump
|
|
116
|
+
useClientLayoutEffect(updateCollapseButtonTop)
|
|
117
|
+
|
|
118
|
+
// the header also resizes without a render, for example when the logo image loads
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
const header = headerRef.current
|
|
121
|
+
if (!header || typeof ResizeObserver === 'undefined') return
|
|
122
|
+
const observer = new ResizeObserver(updateCollapseButtonTop)
|
|
123
|
+
observer.observe(header)
|
|
124
|
+
return () => observer.disconnect()
|
|
125
|
+
}, [])
|
|
126
|
+
|
|
127
|
+
// the SubNav of the current page collapses to a strip, other SubNavs close completely
|
|
128
|
+
const currentSectionItem = items.find((item) => item.current && item.subNav)
|
|
129
|
+
const collapsible =
|
|
130
|
+
!!openItem && openItem.itemID === currentSectionItem?.itemID
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
const intent = focusIntentRef.current
|
|
134
|
+
// an intent lives for one commit, so a change that the app ignores doesn't move focus later
|
|
135
|
+
focusIntentRef.current = undefined
|
|
136
|
+
const closedID = renderedSubNavRef.current
|
|
137
|
+
renderedSubNavRef.current = openItem?.itemID
|
|
138
|
+
|
|
139
|
+
if (
|
|
140
|
+
!openItem &&
|
|
141
|
+
closedID !== undefined &&
|
|
142
|
+
(intent?.target === 'trigger' || intent?.target === 'rescue')
|
|
143
|
+
) {
|
|
144
|
+
triggerRefs.current.get(closedID)?.focus()
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const handleItemActivate = (
|
|
149
|
+
event: MouseEvent<HTMLElement>,
|
|
150
|
+
item: GlobalNavMainNavItem
|
|
151
|
+
) => {
|
|
152
|
+
if (item.subNav) {
|
|
153
|
+
const next = openSubNav === item.itemID ? undefined : item.itemID
|
|
154
|
+
focusIntentRef.current = next
|
|
155
|
+
? { target: 'first' }
|
|
156
|
+
: { target: 'trigger' }
|
|
157
|
+
onSubNavChange?.(event, next)
|
|
158
|
+
} else if (
|
|
159
|
+
openItem &&
|
|
160
|
+
// a link opened in a new tab or window keeps the SubNav open
|
|
161
|
+
!(event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
162
|
+
) {
|
|
163
|
+
focusIntentRef.current = { target: 'none' }
|
|
164
|
+
onSubNavChange?.(event, undefined)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const handleSubNavClose = (event: SyntheticEvent) => {
|
|
169
|
+
focusIntentRef.current = { target: 'trigger' }
|
|
170
|
+
onSubNavChange?.(event, undefined)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const handleSubNavExpand = (event: SyntheticEvent) => {
|
|
174
|
+
if (!currentSectionItem) return
|
|
175
|
+
focusIntentRef.current = { target: 'first' }
|
|
176
|
+
onSubNavChange?.(event, currentSectionItem.itemID)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<nav
|
|
181
|
+
{...omitProps(props, allowedProps)}
|
|
182
|
+
ref={ref}
|
|
183
|
+
aria-label={label}
|
|
184
|
+
css={styles?.globalNav}
|
|
185
|
+
>
|
|
186
|
+
<div ref={railRef} css={styles?.rail}>
|
|
187
|
+
<div css={mainNavStyles?.mainNav}>
|
|
188
|
+
<div ref={headerRef} css={mainNavStyles?.header}>
|
|
189
|
+
<InstitutionLogo logo={logo} collapsed={collapsed} />
|
|
190
|
+
<CollapseButton
|
|
191
|
+
collapsed={collapsed}
|
|
192
|
+
collapseLabel={collapseLabel}
|
|
193
|
+
expandLabel={expandLabel}
|
|
194
|
+
onCollapse={onCollapse}
|
|
195
|
+
elementRef={(element) => {
|
|
196
|
+
collapseButtonRef.current = element
|
|
197
|
+
}}
|
|
198
|
+
/>
|
|
199
|
+
</div>
|
|
200
|
+
{!collapsed && <div css={mainNavStyles?.divider} />}
|
|
201
|
+
<ul css={mainNavStyles?.items}>
|
|
202
|
+
{items.map((item) => (
|
|
203
|
+
<MainNavItem
|
|
204
|
+
key={item.itemID}
|
|
205
|
+
item={item}
|
|
206
|
+
collapsed={collapsed}
|
|
207
|
+
open={openSubNav === item.itemID}
|
|
208
|
+
subNavId={
|
|
209
|
+
openItem?.itemID === item.itemID ? subNavId : undefined
|
|
210
|
+
}
|
|
211
|
+
onActivate={handleItemActivate}
|
|
212
|
+
elementRef={(element) => {
|
|
213
|
+
if (element) {
|
|
214
|
+
triggerRefs.current.set(item.itemID, element)
|
|
215
|
+
} else {
|
|
216
|
+
triggerRefs.current.delete(item.itemID)
|
|
217
|
+
}
|
|
218
|
+
}}
|
|
219
|
+
/>
|
|
220
|
+
))}
|
|
221
|
+
</ul>
|
|
222
|
+
</div>
|
|
223
|
+
{openItem?.subNav ? (
|
|
224
|
+
<SubNav
|
|
225
|
+
key={openItem.itemID}
|
|
226
|
+
subNav={openItem.subNav}
|
|
227
|
+
subNavId={subNavId}
|
|
228
|
+
titleId={titleId}
|
|
229
|
+
collapsible={collapsible}
|
|
230
|
+
closeSubNavLabel={
|
|
231
|
+
collapsible
|
|
232
|
+
? collapseSubNavLabel ?? closeSubNavLabel
|
|
233
|
+
: closeSubNavLabel
|
|
234
|
+
}
|
|
235
|
+
backLabel={backLabel}
|
|
236
|
+
onClose={handleSubNavClose}
|
|
237
|
+
focusIntentRef={focusIntentRef}
|
|
238
|
+
/>
|
|
239
|
+
) : (
|
|
240
|
+
currentSectionItem && (
|
|
241
|
+
<CollapsedSubNav
|
|
242
|
+
label={expandSubNavLabel}
|
|
243
|
+
onExpand={handleSubNavExpand}
|
|
244
|
+
/>
|
|
245
|
+
)
|
|
246
|
+
)}
|
|
247
|
+
</div>
|
|
248
|
+
</nav>
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
GlobalNav.displayName = 'GlobalNav'
|
|
254
|
+
|
|
255
|
+
export default GlobalNav
|
|
256
|
+
export { GlobalNav }
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type {
|
|
26
|
+
AnchorHTMLAttributes,
|
|
27
|
+
ChangeEvent,
|
|
28
|
+
MouseEvent,
|
|
29
|
+
SyntheticEvent
|
|
30
|
+
} from 'react'
|
|
31
|
+
|
|
32
|
+
import type { ComponentStyle, NewThemeOverrideProp } from '@instructure/emotion'
|
|
33
|
+
import type { OtherHTMLAttributes, Renderable } from '@instructure/shared-types'
|
|
34
|
+
import type { NewComponentTypes } from '@instructure/ui-themes'
|
|
35
|
+
|
|
36
|
+
type GlobalNavCountLabel = string | ((count: number) => string)
|
|
37
|
+
|
|
38
|
+
type GlobalNavElementProps = Pick<
|
|
39
|
+
AnchorHTMLAttributes<HTMLElement>,
|
|
40
|
+
'id' | 'className' | 'target' | 'rel'
|
|
41
|
+
> & {
|
|
42
|
+
// object literals typed as HTML attributes reject data-* keys without this
|
|
43
|
+
[key: `data-${string}`]: string | number | boolean | undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type GlobalNavSubNavItem = {
|
|
47
|
+
/**
|
|
48
|
+
* Identifies the item in `openSubNav` and is its React key, but isn't rendered to the DOM.
|
|
49
|
+
*/
|
|
50
|
+
itemID: string
|
|
51
|
+
/**
|
|
52
|
+
* The visible text and accessible name of the item.
|
|
53
|
+
*/
|
|
54
|
+
label: string
|
|
55
|
+
/**
|
|
56
|
+
* The URL of the page, ignored for items with a `subNav`.
|
|
57
|
+
*/
|
|
58
|
+
href?: string
|
|
59
|
+
/**
|
|
60
|
+
* Called when the user activates the item, also when it opens a deeper level.
|
|
61
|
+
*/
|
|
62
|
+
onClick?: (event: MouseEvent<HTMLElement>) => void
|
|
63
|
+
/**
|
|
64
|
+
* Whether the item is the current page, which sets `aria-current="page"`.
|
|
65
|
+
*/
|
|
66
|
+
current?: boolean
|
|
67
|
+
/**
|
|
68
|
+
* The deeper level that the item opens as a disclosure button.
|
|
69
|
+
*/
|
|
70
|
+
subNav?: GlobalNavSubNav
|
|
71
|
+
/**
|
|
72
|
+
* HTML attributes for the item's link or button: `id`, `className`, `target`, `rel` and `data-*`.
|
|
73
|
+
*/
|
|
74
|
+
elementProps?: GlobalNavElementProps
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type GlobalNavMainNavItem = GlobalNavSubNavItem & {
|
|
78
|
+
/**
|
|
79
|
+
* The icon of the rail item.
|
|
80
|
+
*/
|
|
81
|
+
icon: Renderable
|
|
82
|
+
/**
|
|
83
|
+
* The number in the badge, which is hidden when it is 0 or missing.
|
|
84
|
+
*/
|
|
85
|
+
badgeCount?: number
|
|
86
|
+
/**
|
|
87
|
+
* Screen reader text of the badge.
|
|
88
|
+
*/
|
|
89
|
+
badgeLabel?: GlobalNavCountLabel
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type GlobalNavSubNavWithItems = {
|
|
93
|
+
/**
|
|
94
|
+
* The heading of the panel.
|
|
95
|
+
*/
|
|
96
|
+
title: string
|
|
97
|
+
/**
|
|
98
|
+
* The links of the panel, and buttons that open a deeper level.
|
|
99
|
+
*/
|
|
100
|
+
items?: GlobalNavSubNavItem[]
|
|
101
|
+
renderContent?: never
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type GlobalNavSubNavWithContent = {
|
|
105
|
+
/**
|
|
106
|
+
* The heading of the panel.
|
|
107
|
+
*/
|
|
108
|
+
title: string
|
|
109
|
+
/**
|
|
110
|
+
* Any content in place of `items`, inside the panel frame.
|
|
111
|
+
*/
|
|
112
|
+
renderContent: Renderable
|
|
113
|
+
items?: never
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// a SubNav shows either items or custom content, never both
|
|
117
|
+
type GlobalNavSubNav = GlobalNavSubNavWithItems | GlobalNavSubNavWithContent
|
|
118
|
+
|
|
119
|
+
type GlobalNavLogo = {
|
|
120
|
+
/**
|
|
121
|
+
* The URL of the logo image.
|
|
122
|
+
*/
|
|
123
|
+
src?: string
|
|
124
|
+
/**
|
|
125
|
+
* The name of the institution or product, shown next to the image or used as the alternative text of a wordmark.
|
|
126
|
+
*/
|
|
127
|
+
name: string
|
|
128
|
+
/**
|
|
129
|
+
* Whether to show `name` as text. Set it to `false` when `src` is a wide logo that already shows the name, for example a product wordmark. Defaults to `true`.
|
|
130
|
+
*/
|
|
131
|
+
showName?: boolean
|
|
132
|
+
/**
|
|
133
|
+
* The URL that the logo links to.
|
|
134
|
+
*/
|
|
135
|
+
href?: string
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type GlobalNavToggle = {
|
|
139
|
+
/**
|
|
140
|
+
* Identifies the toggle and is its React key.
|
|
141
|
+
*/
|
|
142
|
+
itemID: string
|
|
143
|
+
/**
|
|
144
|
+
* The icon before the label.
|
|
145
|
+
*/
|
|
146
|
+
icon?: Renderable
|
|
147
|
+
/**
|
|
148
|
+
* The visible text and accessible name of the toggle.
|
|
149
|
+
*/
|
|
150
|
+
label: string
|
|
151
|
+
/**
|
|
152
|
+
* Whether the toggle is on.
|
|
153
|
+
*/
|
|
154
|
+
checked: boolean
|
|
155
|
+
/**
|
|
156
|
+
* Called when the user switches the toggle, with the new `checked` value.
|
|
157
|
+
*/
|
|
158
|
+
onChange: (event: ChangeEvent<HTMLInputElement>, checked: boolean) => void
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
type GlobalNavAccount = {
|
|
162
|
+
/**
|
|
163
|
+
* Identifies the account panel in `openSubNav`.
|
|
164
|
+
*/
|
|
165
|
+
itemID: string
|
|
166
|
+
/**
|
|
167
|
+
* The heading of the account panel.
|
|
168
|
+
*/
|
|
169
|
+
label: string
|
|
170
|
+
/**
|
|
171
|
+
* The user's name, shown on the rail item and in the panel.
|
|
172
|
+
*/
|
|
173
|
+
name: string
|
|
174
|
+
/**
|
|
175
|
+
* The URL of the user's avatar.
|
|
176
|
+
*/
|
|
177
|
+
avatarSrc?: string
|
|
178
|
+
/**
|
|
179
|
+
* The text of the role pill.
|
|
180
|
+
*/
|
|
181
|
+
userType?: string
|
|
182
|
+
/**
|
|
183
|
+
* The name of the user's institution.
|
|
184
|
+
*/
|
|
185
|
+
institution?: string
|
|
186
|
+
/**
|
|
187
|
+
* The number in the badge of the rail item, which is hidden when it is 0 or missing.
|
|
188
|
+
*/
|
|
189
|
+
badgeCount?: number
|
|
190
|
+
/**
|
|
191
|
+
* Screen reader text of the badge.
|
|
192
|
+
*/
|
|
193
|
+
badgeLabel?: GlobalNavCountLabel
|
|
194
|
+
/**
|
|
195
|
+
* Whether the panel items are loading.
|
|
196
|
+
*/
|
|
197
|
+
loading?: boolean
|
|
198
|
+
/**
|
|
199
|
+
* Screen reader text of the loading spinner.
|
|
200
|
+
*/
|
|
201
|
+
loadingLabel?: string
|
|
202
|
+
/**
|
|
203
|
+
* The links of the panel.
|
|
204
|
+
*/
|
|
205
|
+
items?: GlobalNavSubNavItem[]
|
|
206
|
+
/**
|
|
207
|
+
* A section of toggles with a heading, for example user interface settings.
|
|
208
|
+
*/
|
|
209
|
+
toggles?: {
|
|
210
|
+
label: string
|
|
211
|
+
items: GlobalNavToggle[]
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Any content, rendered after the toggles.
|
|
215
|
+
*/
|
|
216
|
+
renderContent?: Renderable
|
|
217
|
+
/**
|
|
218
|
+
* The log out button at the end of the panel.
|
|
219
|
+
*/
|
|
220
|
+
logout?: {
|
|
221
|
+
label: string
|
|
222
|
+
href?: string
|
|
223
|
+
onClick?: (event: MouseEvent<HTMLElement>) => void
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
type GlobalNavOwnProps = {
|
|
228
|
+
/**
|
|
229
|
+
* Accessible name of the navigation landmark, read by screen readers.
|
|
230
|
+
*/
|
|
231
|
+
label: string
|
|
232
|
+
/**
|
|
233
|
+
* Whether the rail is collapsed, a state that the app updates in `onCollapse`.
|
|
234
|
+
*/
|
|
235
|
+
collapsed: boolean
|
|
236
|
+
/**
|
|
237
|
+
* Called when the user clicks the collapse button, with the new `collapsed` value.
|
|
238
|
+
*/
|
|
239
|
+
onCollapse: (event: SyntheticEvent, collapsed: boolean) => void
|
|
240
|
+
/**
|
|
241
|
+
* Screen reader text of the collapse button when the rail is expanded.
|
|
242
|
+
*/
|
|
243
|
+
collapseLabel: string
|
|
244
|
+
/**
|
|
245
|
+
* Screen reader text of the collapse button when the rail is collapsed.
|
|
246
|
+
*/
|
|
247
|
+
expandLabel: string
|
|
248
|
+
/**
|
|
249
|
+
* Screen reader text of the back button in a deeper SubNav level, from the title of the level it returns to.
|
|
250
|
+
*/
|
|
251
|
+
backLabel: (parentTitle: string) => string
|
|
252
|
+
/**
|
|
253
|
+
* Screen reader text of the button that closes a SubNav.
|
|
254
|
+
*/
|
|
255
|
+
closeSubNavLabel: string
|
|
256
|
+
/**
|
|
257
|
+
* Screen reader text of the handle that collapses the current page's SubNav to the strip, `closeSubNavLabel` if not set.
|
|
258
|
+
*/
|
|
259
|
+
collapseSubNavLabel?: string
|
|
260
|
+
/**
|
|
261
|
+
* Screen reader text of the button that opens the SubNav of the current page again.
|
|
262
|
+
*/
|
|
263
|
+
expandSubNavLabel: string
|
|
264
|
+
/**
|
|
265
|
+
* The `itemID` of the item or account whose SubNav is open, or `undefined` when none is open.
|
|
266
|
+
*/
|
|
267
|
+
openSubNav?: string
|
|
268
|
+
/**
|
|
269
|
+
* Called when the user opens or closes a SubNav, with the `itemID` of the item or `undefined`.
|
|
270
|
+
*/
|
|
271
|
+
onSubNavChange?: (event: SyntheticEvent, itemID: string | undefined) => void
|
|
272
|
+
/**
|
|
273
|
+
* The rail items, top to bottom.
|
|
274
|
+
*/
|
|
275
|
+
items: GlobalNavMainNavItem[]
|
|
276
|
+
/**
|
|
277
|
+
* The institution logo at the top of the rail.
|
|
278
|
+
*/
|
|
279
|
+
logo: GlobalNavLogo
|
|
280
|
+
/**
|
|
281
|
+
* The account item of the rail and its panel.
|
|
282
|
+
*/
|
|
283
|
+
account?: GlobalNavAccount
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
type PropKeys = keyof GlobalNavOwnProps
|
|
287
|
+
|
|
288
|
+
type AllowedPropKeys = Readonly<Array<PropKeys>>
|
|
289
|
+
|
|
290
|
+
type GlobalNavProps = GlobalNavOwnProps &
|
|
291
|
+
NewThemeOverrideProp<ReturnType<NewComponentTypes['GlobalNav']>> &
|
|
292
|
+
OtherHTMLAttributes<GlobalNavOwnProps>
|
|
293
|
+
|
|
294
|
+
type GlobalNavStyle = ComponentStyle<'globalNav' | 'rail'>
|
|
295
|
+
|
|
296
|
+
type MainNavStyle = ComponentStyle<'mainNav' | 'header' | 'items' | 'divider'>
|
|
297
|
+
|
|
298
|
+
const allowedProps: AllowedPropKeys = [
|
|
299
|
+
'label',
|
|
300
|
+
'collapsed',
|
|
301
|
+
'onCollapse',
|
|
302
|
+
'collapseLabel',
|
|
303
|
+
'expandLabel',
|
|
304
|
+
'backLabel',
|
|
305
|
+
'closeSubNavLabel',
|
|
306
|
+
'collapseSubNavLabel',
|
|
307
|
+
'expandSubNavLabel',
|
|
308
|
+
'openSubNav',
|
|
309
|
+
'onSubNavChange',
|
|
310
|
+
'items',
|
|
311
|
+
'logo',
|
|
312
|
+
'account'
|
|
313
|
+
]
|
|
314
|
+
|
|
315
|
+
export type {
|
|
316
|
+
GlobalNavProps,
|
|
317
|
+
GlobalNavStyle,
|
|
318
|
+
MainNavStyle,
|
|
319
|
+
GlobalNavMainNavItem,
|
|
320
|
+
GlobalNavSubNav,
|
|
321
|
+
GlobalNavSubNavItem,
|
|
322
|
+
GlobalNavLogo,
|
|
323
|
+
GlobalNavAccount,
|
|
324
|
+
GlobalNavToggle,
|
|
325
|
+
GlobalNavCountLabel,
|
|
326
|
+
GlobalNavElementProps
|
|
327
|
+
}
|
|
328
|
+
export { allowedProps }
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { boxShadowObjectsToCSSString } from '@instructure/ui-themes'
|
|
26
|
+
import type { NewComponentTypes } from '@instructure/ui-themes'
|
|
27
|
+
import type { GlobalNavStyle, MainNavStyle } from './props.js'
|
|
28
|
+
|
|
29
|
+
type StyleParams = {
|
|
30
|
+
collapsed: boolean
|
|
31
|
+
subNavOpen: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* ---
|
|
36
|
+
* private: true
|
|
37
|
+
* ---
|
|
38
|
+
* Generates the style object from the theme and provided additional information
|
|
39
|
+
* @param componentTheme The theme variable object.
|
|
40
|
+
* @param params Additional parameters to customize the style.
|
|
41
|
+
* @return The final style object, which will be used in the component
|
|
42
|
+
*/
|
|
43
|
+
const generateStyle = (
|
|
44
|
+
componentTheme: ReturnType<NewComponentTypes['GlobalNav']>,
|
|
45
|
+
params: StyleParams
|
|
46
|
+
): GlobalNavStyle => {
|
|
47
|
+
const { collapsed, subNavOpen } = params
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
globalNav: {
|
|
51
|
+
label: 'globalNav',
|
|
52
|
+
display: 'flex',
|
|
53
|
+
// fixed while no SubNav is open, so showing the strip doesn't move the page
|
|
54
|
+
width: subNavOpen
|
|
55
|
+
? 'fit-content'
|
|
56
|
+
: collapsed
|
|
57
|
+
? componentTheme.minimizedminWidth
|
|
58
|
+
: componentTheme.minWidth,
|
|
59
|
+
flexShrink: 0,
|
|
60
|
+
height: '100%',
|
|
61
|
+
boxSizing: 'border-box',
|
|
62
|
+
// the width tokens include the outer gap, which is this padding
|
|
63
|
+
padding: collapsed
|
|
64
|
+
? componentTheme.minimizedMargin
|
|
65
|
+
: componentTheme.margin
|
|
66
|
+
},
|
|
67
|
+
rail: {
|
|
68
|
+
label: 'globalNav__rail',
|
|
69
|
+
display: 'flex',
|
|
70
|
+
// the rail can be wider than the width tokens, so it overflows into the padding instead of shrinking
|
|
71
|
+
flexShrink: 0,
|
|
72
|
+
boxSizing: 'border-box',
|
|
73
|
+
backgroundColor: componentTheme.backgroundColor,
|
|
74
|
+
border: `${componentTheme.borderWidth} solid ${componentTheme.borderColor}`,
|
|
75
|
+
borderRadius: collapsed
|
|
76
|
+
? componentTheme.minimizedBorderRadius
|
|
77
|
+
: componentTheme.borderRadius,
|
|
78
|
+
boxShadow: boxShadowObjectsToCSSString(componentTheme.boxShadow)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Generates the style object of the main column from the `MainNav` tokens
|
|
85
|
+
*/
|
|
86
|
+
const generateMainNavStyle = (
|
|
87
|
+
componentTheme: ReturnType<NewComponentTypes['MainNav']>,
|
|
88
|
+
params: Pick<StyleParams, 'collapsed'>
|
|
89
|
+
): MainNavStyle => {
|
|
90
|
+
const { collapsed } = params
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
mainNav: {
|
|
94
|
+
label: 'mainNav',
|
|
95
|
+
width: collapsed ? componentTheme.minimizedWidth : componentTheme.width,
|
|
96
|
+
flexShrink: 0,
|
|
97
|
+
boxSizing: 'border-box',
|
|
98
|
+
display: 'flex',
|
|
99
|
+
flexDirection: 'column',
|
|
100
|
+
gap: collapsed ? componentTheme.minimizedGap : componentTheme.gap,
|
|
101
|
+
padding: collapsed
|
|
102
|
+
? `${componentTheme.minimizedGap} ${componentTheme.minimizedMargin}`
|
|
103
|
+
: componentTheme.margin
|
|
104
|
+
},
|
|
105
|
+
header: {
|
|
106
|
+
label: 'mainNav__header',
|
|
107
|
+
display: 'flex',
|
|
108
|
+
flexDirection: collapsed ? 'column' : 'row',
|
|
109
|
+
alignItems: 'center',
|
|
110
|
+
justifyContent: collapsed ? 'center' : 'flex-end',
|
|
111
|
+
gap: collapsed ? componentTheme.minimizedGap : componentTheme.gap
|
|
112
|
+
},
|
|
113
|
+
items: {
|
|
114
|
+
label: 'mainNav__items',
|
|
115
|
+
display: 'flex',
|
|
116
|
+
flexDirection: 'column',
|
|
117
|
+
alignItems: collapsed ? 'center' : 'stretch',
|
|
118
|
+
gap: componentTheme.navItemsGap,
|
|
119
|
+
listStyle: 'none',
|
|
120
|
+
margin: 0,
|
|
121
|
+
padding: 0
|
|
122
|
+
},
|
|
123
|
+
divider: {
|
|
124
|
+
label: 'mainNav__divider',
|
|
125
|
+
// MainNav has no divider width token
|
|
126
|
+
borderTop: `0.0625rem solid ${componentTheme.strokeColor}`
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export default generateStyle
|
|
132
|
+
export { generateMainNavStyle }
|