-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathbottom-nav.svelte
More file actions
206 lines (184 loc) · 6.09 KB
/
bottom-nav.svelte
File metadata and controls
206 lines (184 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<script lang="ts">
import { writable } from 'svelte/store';
import { slide } from 'svelte/transition';
import type { Snippet } from 'svelte';
import { twMerge as merge } from 'tailwind-merge';
import { beforeNavigate } from '$app/navigation';
import { page } from '$app/state';
import Button from '$lib/holocene/button.svelte';
import Icon from '$lib/holocene/icon/icon.svelte';
import Logo from '$lib/holocene/logo.svelte';
import { translate } from '$lib/i18n/translate';
import { lastUsedNamespace } from '$lib/stores/namespaces';
import type { NamespaceListItem, NavLinkListItem } from '$lib/types/global';
import { routeForNamespace } from '$lib/utilities/route-for';
import BottomNavLinks from './bottom-nav-links.svelte';
import BottomNavSettings from './bottom-nav-settings.svelte';
type Props = {
children: Snippet;
namespacePicker: Snippet<[{ open: boolean; closeMenu: () => void }]>;
avatar: Snippet;
namespaceList?: NamespaceListItem[];
isCloud: boolean;
linkList: NavLinkListItem[];
showNamespacePicker?: boolean;
environmentName?: string;
};
let {
children,
namespacePicker,
avatar,
namespaceList = [],
isCloud = false,
linkList,
showNamespacePicker = true,
environmentName,
}: Props = $props();
let viewLinks = $state(false);
let viewNamespaces = writable(false);
let viewSettings = $state(false);
function escapeHandler(e: KeyboardEvent) {
if (
e.key === 'Escape' &&
[viewLinks, viewNamespaces, viewSettings].some((isOpen) => isOpen)
) {
closeMenu();
}
}
beforeNavigate(() => {
closeMenu();
});
const namespace = $derived(page.params.namespace || $lastUsedNamespace);
const namespaceExists = $derived(
namespaceList.some(
(namespaceListItem) => namespaceListItem.namespace === namespace,
),
);
const menuIsOpen = $derived(viewLinks || $viewNamespaces || viewSettings);
const onLinksClick = () => {
viewLinks = !viewLinks;
$viewNamespaces = false;
viewSettings = false;
};
const onNamespaceClick = () => {
viewLinks = false;
$viewNamespaces = !$viewNamespaces;
viewSettings = false;
};
const onSettingsClick = () => {
viewLinks = false;
$viewNamespaces = false;
viewSettings = !viewSettings;
};
function closeMenu() {
viewLinks = false;
$viewNamespaces = false;
viewSettings = false;
}
const truncateNamespace = (namespace: string) => {
if (namespace.length > 16) {
return `${namespace.slice(0, 8)}...${namespace.slice(-8)}`;
}
return namespace;
};
</script>
<svelte:window on:keypress={escapeHandler} />
{#if menuIsOpen}
<div
class={merge(
'group fixed top-0 z-50 h-[calc(100%-64px)] w-full overflow-auto md:hidden',
'focus-visible:[&_[role=button]]:outline-none focus-visible:[&_[role=button]]:ring-2 focus-visible:[&_[role=button]]:ring-primary/70 focus-visible:[&_a]:outline-none focus-visible:[&_a]:ring-2 focus-visible:[&_a]:ring-primary/70',
isCloud
? 'bg-gradient-to-b from-indigo-600 to-indigo-950 text-off-white focus-visible:[&_[role=button]]:ring-success focus-visible:[&_a]:ring-success'
: 'surface-black',
)}
data-nav="open"
in:slide={{ duration: 200, delay: 0 }}
out:slide={{ duration: 200, delay: 0 }}
>
<BottomNavLinks open={viewLinks} {linkList} />
{@render namespacePicker({ open: $viewNamespaces, closeMenu })}
<BottomNavSettings open={viewSettings}>
{@render children?.()}
</BottomNavSettings>
</div>
{/if}
<nav
class={merge(
'fixed bottom-0 z-40 flex h-[64px] w-full flex-row items-center justify-between gap-5 px-4 py-2 transition-colors md:hidden',
'focus-visible:[&_a]:outline-none focus-visible:[&_a]:ring-2 focus-visible:[&_a]:ring-primary/70 focus-visible:[&_button]:outline-none focus-visible:[&_button]:ring-2 focus-visible:[&_button]:ring-primary/70',
isCloud
? 'bg-gradient-to-b from-indigo-600 to-indigo-900 text-off-white focus-visible:[&_a]:ring-success focus-visible:[&_button]:ring-success'
: environmentName || 'surface-black border-t border-subtle',
)}
data-testid="top-nav"
aria-label={translate('common.main')}
>
<button
class="nav-button relative"
data-testid="nav-menu-button"
class:active-shadow={viewLinks}
type="button"
onclick={onLinksClick}
>
{#if viewLinks}
<Icon name="close" height={32} width={32} />
{:else}
<Logo height={32} width={32} />
{/if}
</button>
{#if showNamespacePicker}
<div class="namespace-wrapper">
<Button
variant="ghost"
data-testid="namespace-switcher"
leadingIcon="namespace-switcher"
size="xs"
class="grow text-white"
on:click={onNamespaceClick}>{truncateNamespace(namespace)}</Button
>
<div class="ml-1 h-full w-1 border-l border-subtle"></div>
<Button
variant="ghost"
size="xs"
href={routeForNamespace({ namespace })}
disabled={!namespaceExists}
><Icon class="text-white" name="external-link" /></Button
>
</div>
{/if}
<button
class="nav-button"
data-testid="nav-profile-button"
class:active-shadow={viewSettings}
type="button"
onclick={onSettingsClick}
>
{#if viewSettings}
<Icon name="close" height={32} width={32} />
{:else}
<div
class="flex aspect-square w-[32px] min-w-[32px] items-center justify-center"
>
{@render avatar()}
</div>
{/if}
</button>
</nav>
<style lang="postcss">
.namespace-wrapper {
@apply surface-black flex h-10 w-full grow flex-row items-center border border-subtle px-0.5 text-sm dark:focus-within:surface-primary focus-within:border-interactive focus-within:outline-none focus-within:ring-2 focus-within:ring-primary/70;
}
.nav-button {
@apply relative select-none p-1 text-center align-middle text-xs font-medium uppercase transition-all;
}
.development {
@apply surface-development border-t border-subtle;
}
.staging {
@apply surface-staging border-t border-subtle;
}
.test {
@apply surface-test border-t border-subtle;
}
</style>