forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavBase.vue
841 lines (749 loc) · 21.2 KB
/
NavBase.vue
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
<!--
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
-->
<template>
<nav
class="nav"
:class="rootClasses"
role="navigation"
ref="nav"
>
<div class="nav__wrapper" ref="wrapper">
<div class="nav__background" />
<div v-if="hasOverlay" class="nav-overlay" @click="closeNav" />
<div class="nav-content">
<slot
name="pre-title"
className="pre-title"
v-bind="{ closeNav, inBreakpoint, currentBreakpoint, isOpen }"
/>
<div v-if="$slots.default" class="nav-title">
<slot />
</div>
<slot name="after-title" />
<div class="nav-menu">
<a
href="#"
class="nav-ax-toggle"
ref="axToggle"
role="button"
@click.prevent="toggleNav"
>
<span class="visuallyhidden">
<template v-if="!isOpen">
{{ $t('documentation.nav.open-menu') }}
</template>
<template v-else>
{{ $t('documentation.nav.close-menu') }}
</template>
</span>
</a>
<div
ref="tray"
class="nav-menu-tray"
@transitionend.self="onTransitionEnd"
@click="handleTrayClick"
>
<slot name="tray" :closeNav="closeNav">
<NavMenuItems>
<slot name="menu-items" />
</NavMenuItems>
</slot>
</div>
</div>
<div v-if="showActions" class="nav-actions">
<a
ref="toggle"
href="#"
class="nav-menucta"
tabindex="-1"
@click.prevent="toggleNav"
aria-hidden="true"
>
<span class="nav-menucta-chevron" />
</a>
</div>
</div>
<slot name="after-content" />
</div>
<BreakpointEmitter :scope="BreakpointScopes.nav" @change="onBreakpointChange" />
</nav>
</template>
<script>
import onIntersect from 'docc-render/mixins/onIntersect';
import NavMenuItems from 'docc-render/components/NavMenuItems.vue';
import BreakpointEmitter from 'docc-render/components/BreakpointEmitter.vue';
import scrollLock from 'docc-render/utils/scroll-lock';
import { baseNavStickyAnchorId, MenuLinkModifierClasses } from 'docc-render/constants/nav';
import { isBreakpointAbove } from 'docc-render/utils/breakpoints';
import changeElementVOVisibility from 'docc-render/utils/changeElementVOVisibility';
import { waitFrames } from 'docc-render/utils/loading';
const { noClose } = MenuLinkModifierClasses;
const { BreakpointName, BreakpointScopes } = BreakpointEmitter.constants;
const NoBGTransitionFrames = 8;
const NavStateClasses = {
isDark: 'theme-dark',
isOpen: 'nav--is-open',
inBreakpoint: 'nav--in-breakpoint-range',
isTransitioning: 'nav--is-transitioning',
isSticking: 'nav--is-sticking',
hasSolidBackground: 'nav--solid-background',
hasNoBorder: 'nav--noborder',
hasFullWidthBorder: 'nav--fullwidth-border',
isWideFormat: 'nav--is-wide-format',
noBackgroundTransition: 'nav--no-bg-transition',
};
export default {
name: 'NavBase',
components: { NavMenuItems, BreakpointEmitter },
constants: { NavStateClasses, NoBGTransitionFrames },
props: {
/**
* At which breakpoint size should the nav transform to a mobile friendly navigation.
* It's mostly set to `small` with the Documentation pages being an exception with `medium`.
*/
breakpoint: {
type: String,
default: BreakpointName.small,
},
/**
* Should the mobile navigation render a dark overlay when opened.
*/
hasOverlay: {
type: Boolean,
default: true,
},
/**
* Whether the navigation has a constant solid background, without transparency or blur.
*/
hasSolidBackground: {
type: Boolean,
default: false,
},
/**
* Whether the navigation has a bottom border
*/
hasNoBorder: {
type: Boolean,
default: false,
},
hasFullWidthBorder: {
type: Boolean,
default: false,
},
isDark: {
type: Boolean,
default: false,
},
isWideFormat: {
type: Boolean,
default: false,
},
showActions: {
type: Boolean,
default: true,
},
},
mixins: [onIntersect],
data() {
return {
isOpen: false,
isTransitioning: false,
isSticking: false,
noBackgroundTransition: true,
currentBreakpoint: BreakpointName.large,
};
},
computed: {
BreakpointScopes: () => BreakpointScopes,
inBreakpoint: ({ currentBreakpoint, breakpoint }) => (
!isBreakpointAbove(currentBreakpoint, breakpoint)
),
rootClasses: ({
isOpen, inBreakpoint, isTransitioning, isSticking, hasSolidBackground,
hasNoBorder, hasFullWidthBorder, isDark, isWideFormat, noBackgroundTransition,
}) => ({
[NavStateClasses.isDark]: isDark,
[NavStateClasses.isOpen]: isOpen,
[NavStateClasses.inBreakpoint]: inBreakpoint,
[NavStateClasses.isTransitioning]: isTransitioning,
[NavStateClasses.isSticking]: isSticking,
[NavStateClasses.hasSolidBackground]: hasSolidBackground,
[NavStateClasses.hasNoBorder]: hasNoBorder,
[NavStateClasses.hasFullWidthBorder]: hasFullWidthBorder,
[NavStateClasses.isWideFormat]: isWideFormat,
[NavStateClasses.noBackgroundTransition]: noBackgroundTransition,
}),
},
watch: {
isOpen(value) {
this.$emit('change', value);
if (value) {
this.onExpand();
} else {
this.onClose();
}
},
},
async mounted() {
window.addEventListener('keydown', this.onEscape);
window.addEventListener('popstate', this.closeNav);
window.addEventListener('orientationchange', this.closeNav);
document.addEventListener('click', this.handleClickOutside);
this.handleFlashOnMount();
await this.$nextTick();
},
beforeDestroy() {
window.removeEventListener('keydown', this.onEscape);
window.removeEventListener('popstate', this.closeNav);
window.removeEventListener('orientationchange', this.closeNav);
document.removeEventListener('click', this.handleClickOutside);
if (this.isOpen) {
this.toggleScrollLock(false);
}
},
methods: {
getIntersectionTargets() {
return [document.getElementById(baseNavStickyAnchorId) || this.$el];
},
toggleNav() {
this.isOpen = !this.isOpen;
this.isTransitioning = true;
},
closeNav() {
const oldValue = this.isOpen;
// close the nav
this.isOpen = false;
// return a promise, that resolves when transitions end
return this.resolveOnceTransitionsEnd(oldValue);
},
resolveOnceTransitionsEnd(oldIsOpen) {
// if outside the breakpoint, or was already closed, resolve as there is no tray animation
if (!oldIsOpen || !this.inBreakpoint) return Promise.resolve();
// enable the transitioning up tracking
this.isTransitioning = true;
// resolve a promise, when we stop transitioning
return new Promise((resolve) => {
const unwatch = this.$watch('isTransitioning', () => {
resolve();
unwatch();
});
});
},
/**
* When the closing animation ends,
* allow the menu on mobile to be scrollable.
*/
async onTransitionEnd({ propertyName }) {
if (propertyName !== 'max-height') return;
this.$emit('changed', this.isOpen);
this.isTransitioning = false;
if (this.isOpen) {
this.$emit('opened');
// toggle the scroll lock on/off if needed
this.toggleScrollLock(true);
} else {
this.$emit('closed');
}
},
/**
* Detect if the nav is inside the desired breakpoint
* @param breakpoint
*/
onBreakpointChange(breakpoint) {
this.currentBreakpoint = breakpoint;
// close the nav if outside of the breakpoint
if (!this.inBreakpoint) this.closeNav();
},
/**
* On every intersection change
* update the `is-sticking` class.
* @param {IntersectionObserverEntry} entry
* @param {Number} entry.intersectionRatio
*/
onIntersect({ intersectionRatio }) {
// if the page is pulled down, the elastic effect can cause false results
if (window.scrollY < 0) return;
// If nav is sticking to the top,
// the ratio is below 1, because its outside the 1px margin.
this.isSticking = intersectionRatio !== 1;
},
/**
* On Escape, close the nav and focus the AX Toggle.
*/
onEscape({ key }) {
// react only to escape key and when its expanded
if (key === 'Escape' && this.isOpen) {
this.closeNav();
this.$refs.axToggle.focus();
}
},
/**
* Handle clicks on the tray or any element inside it
* @param {Event} event
* @param {EventTarget} event.target
*/
handleTrayClick({ target }) {
// If the target is a link and has a `href` property, close the nav.
// Targets can opt out of this default behavior with the "noclose" class.
if (target.href && !target.classList.contains(noClose)) {
this.closeNav();
}
},
/**
* Closes the nav, if clicking outside of it.
* @param {EventTarget} target
*/
handleClickOutside({ target }) {
if (this.$refs.nav.contains(target)) return;
this.closeNav();
},
/**
* Toggles the scroll lock on/off,
* only if the tray is scrollable in Y.
*/
toggleScrollLock(lock) {
if (lock) {
scrollLock.lockScroll(this.$refs.tray);
} else {
scrollLock.unlockScroll(this.$refs.tray);
}
},
onExpand() {
this.$emit('open');
// hide sibling elements from VO
changeElementVOVisibility.hide(this.$refs.wrapper);
if (document.activeElement === this.$refs.toggle) {
// move focus to body to prevent tabbing to links in body
// when toggle is triggered by mouse
document.activeElement.blur();
}
},
onClose() {
this.$emit('close');
// stop the scroll lock
this.toggleScrollLock(false);
changeElementVOVisibility.show(this.$refs.wrapper);
},
async handleFlashOnMount() {
await waitFrames(NoBGTransitionFrames);
this.noBackgroundTransition = false;
},
},
};
</script>
<style lang='scss' scoped>
@import "docc-render/styles/_core.scss";
@mixin nav-keyline-color($color) {
&:after {
background-color: $color;
}
}
$content-max-width: map-deep-get($breakpoint-attributes, (nav, large, content-width));
.nav {
position: sticky;
top: 0;
width: 100%;
height: $nav-height;
z-index: $nav-z-index;
--nav-padding: #{$nav-padding};
@media print {
position: relative;
}
@include breakpoint(small, $scope: nav) {
--nav-padding: #{$nav-padding-small};
min-width: map-deep-get($breakpoint-attributes, (nav, small, min-width));
height: $nav-height-small;
}
color: var(--color-nav-color);
@include nav-dark($nested: true) {
background: none;
color: var(--color-nav-dark-color);
}
&__wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: auto;
min-height: 100%;
z-index: 1;
}
&__background {
@include nav-keyline-color(var(--color-nav-keyline));
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
transition: background-color $nav-bg-color-transition;
background-color: var(--color-nav-background, none);
@include nav-is-sticking($nested: true) {
background-color: none;
}
// apply a no-transition, for cases where the nav is sticked at page load,
// removes a nasty flash in the background.
.nav--no-bg-transition & {
transition: none !important;
}
// nav has a solid fill background
@include unify-selector('.nav--solid-background') {
background-color: var(--color-nav-solid-background);
backdrop-filter: none;
@include nav-is-sticking($nested: true) {
background-color: var(--color-nav-solid-background);
}
@include nav-is-open($nested: true) {
background-color: var(--color-nav-solid-background);
}
@include nav-dark($nested: true) {
background-color: var(--color-nav-dark-solid-background);
@include nav-is-sticking($nested: true) {
background-color: var(--color-nav-dark-solid-background);
}
@include nav-is-open($nested: true) {
background-color: var(--color-nav-dark-solid-background);
}
}
}
// nav is collapsed
@include nav-in-breakpoint {
min-height: $nav-height-small;
transition: background-color $nav-bg-transition-timing ease 0.7s;
}
// nav is sticky
@include nav-is-sticking {
@include nav-keyline-color(var(--color-nav-sticking-expanded-keyline));
background-color: var(--color-nav-expanded);
max-height: none;
transition: background-color $nav-bg-transition-timing ease;
transition-property: background-color, backdrop-filter;
@supports (backdrop-filter: initial) {
@include backdrop-filter-uiblur;
background-color: var(--color-nav-uiblur-stuck);
}
// sticky and dark mode
@include nav-dark($nested: true) {
background-color: var(--color-nav-dark-stuck);
@supports (backdrop-filter: initial) {
background-color: var(--color-nav-dark-uiblur-stuck);
}
}
}
// nav is expanded
@include nav-is-open {
@include nav-keyline-color(var(--color-nav-sticking-expanded-keyline));
background-color: var(--color-nav-expanded);
max-height: none;
transition: background-color $nav-bg-transition-timing ease;
transition-property: background-color, backdrop-filter;
@supports (backdrop-filter: initial) {
@include backdrop-filter-uiblur;
background-color: var(--color-nav-uiblur-expanded);
}
@include nav-dark($nested: true) {
background-color: var(--color-nav-dark-expanded);
@supports (backdrop-filter: initial) {
background-color: var(--color-nav-dark-uiblur-expanded);
}
}
}
// in dark mode
@include nav-dark {
@include nav-keyline-color(var(--color-nav-dark-keyline));
@include nav-is-sticking($nested: true) {
@include nav-keyline-color(var(--color-nav-dark-sticking-expanded-keyline));
}
@include nav-is-open($nested: true) {
@include nav-keyline-color(var(--color-nav-dark-sticking-expanded-keyline));
}
}
// the border under the nav
&:after {
content: "";
display: block;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: $content-max-width;
height: 1px;
z-index: 1;
@include breakpoint(medium, $scope: nav) {
width: 100%;
}
@include unify-selector('.nav--noborder') {
display: none;
@include nav-is-sticking($nested: true) {
display: block;
}
}
@include unify-selector('.nav--fullwidth-border') {
width: 100%;
}
@include nav-is-sticking {
width: 100%;
}
@include nav-is-open {
width: 100%;
}
.nav--solid-background & {
width: 100%;
}
}
}
}
// Nav overlay when collapsed and expanded
.nav-overlay {
position: fixed;
left: 0;
right: 0;
top: 0;
display: block;
opacity: 0;
// when the nav is expanded
@include nav-is-open {
background-color: rgba(51, 51, 51, 0.4);
transition: opacity 0.7s $nav-items-timingfunction 0.2s;
bottom: 0;
opacity: 1;
}
}
.nav-wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: auto;
min-height: 100%;
z-index: 1;
}
.pre-title {
display: flex;
}
// Nav content (title and menus)
.nav-content {
display: flex;
padding: 0 var(--nav-padding);
max-width: $content-max-width;
margin: 0 auto;
position: relative;
z-index: 2;
justify-content: space-between;
@include nav-is-wide-format() {
box-sizing: border-box;
@include breakpoint-full-width-container()
}
// Fix iPhone Notch issues
@supports (padding:calc(max(0px))) {
padding-left: calc(max(var(--nav-padding), env(safe-area-inset-left)));
padding-right: calc(max(var(--nav-padding), env(safe-area-inset-right)));
}
@include nav-in-breakpoint {
display: grid;
grid-template-columns: auto 1fr auto;
grid-auto-rows: minmax(min-content, max-content);
grid-template-areas:
"pre-title title actions"
"menu menu menu";
}
}
.nav-menu {
@include font-styles(nav-menu);
flex: 1 1 auto;
display: flex;
justify-content: flex-end;
min-width: 0;
@include nav-in-breakpoint {
@include font-styles(nav-menu-collapsible);
grid-area: menu;
}
}
.nav-menu-tray {
align-items: center;
display: flex;
justify-content: space-between;
@include nav-in-breakpoint {
display: block;
overflow: hidden;
pointer-events: none;
visibility: hidden;
max-height: 0;
transition: $nav-menu-tray-transition;
width: 100%;
@include nav-is-open($nested: true) {
$max-height: calc(100vh - #{$nav-height-small} - #{$nav-height-small});
max-height: $max-height;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
pointer-events: auto;
visibility: visible;
transition-delay: 0.2s, 0s;
@include unify-selector('.nav--is-transitioning', $nested: true) {
overflow-y: hidden;
}
@include nav-is-sticking($nested: true) {
max-height: calc(100vh - #{$nav-height-small});
}
}
}
}
.nav-actions {
display: flex;
align-items: center;
@include nav-in-breakpoint {
grid-area: actions;
justify-content: flex-end;
}
}
.pre-title + .nav-title {
@include nav-in-breakpoint {
grid-area: title;
@include nav-is-wide-format(true) {
width: 100%;
}
}
}
.nav-title {
height: $nav-height;
@include font-styles(nav-title);
display: flex;
align-items: center;
white-space: nowrap;
box-sizing: border-box;
@include breakpoint(small, $scope: nav) {
padding-top: 0;
height: $nav-height-small;
width: 90%;
}
:deep(span) {
line-height: initial;
}
a {
letter-spacing: inherit;
line-height: initial;
margin: 0;
text-decoration: none;
white-space: nowrap;
&:hover {
text-decoration: none;
}
@include breakpoint(small, $scope: nav) {
display: flex;
}
}
&, a {
color: var(--color-figure-gray);
transition: color $nav-bg-color-transition;
@include nav-dark {
color: var(--color-nav-dark-link-color);
@include nav-is-sticking($nested: true) {
color: var(--color-nav-dark-link-color);
}
@include nav-is-open($nested: true) {
color: var(--color-nav-dark-link-color);
}
}
}
}
// element used only with screen readers,
// toggles the nav on/off
.nav-ax-toggle {
display: none;
position: absolute;
top: 0;
left: 0;
width: 1px;
height: 1px;
z-index: 10;
// when in focus, spread out across the entire navigation
&:focus {
outline-offset: -6px;
width: 100%;
height: 100%;
}
// make sure its only visible to VO, in the collapse range.
@include nav-in-breakpoint {
display: block;
}
}
.nav-menucta {
cursor: pointer;
display: none;
align-items: center;
overflow: hidden;
width: rem(20px);
-webkit-tap-highlight-color: transparent;
height: $nav-height-small;
@include nav-in-breakpoint {
display: flex;
}
&-chevron {
display: block;
position: relative;
width: 100%;
height: rem(12px);
transition: $nav-chevron-transition;
&::before,
&::after {
content: "";
display: block;
position: absolute;
top: rem(10px);
width: rem(12px);
height: rem(1px);
transition: $nav-chevron-transition;
background: var(--color-figure-gray);
}
&::before {
right: 50%;
border-radius: .5px 0 0 .5px;
transform: rotate(#{$nav-chevron-angle}) scaleY($nav-chevron-thickness);
}
&::after {
left: 50%;
border-radius: 0 .5px .5px 0;
transform: rotate(-#{$nav-chevron-angle}) scaleY($nav-chevron-thickness);
}
@include nav-chevron-animation;
@include nav-dark {
&::before,
&::after {
background: var(--color-nav-dark-link-color);
}
}
}
}
// Applies colors to any link inside the nav
:deep(.nav-menu-link) {
color: var(--color-nav-link-color);
&:hover {
color: var(--color-nav-link-color-hover);
text-decoration: none;
}
@include nav-dark {
color: var(--color-nav-dark-link-color);
&:hover {
color: var(--color-nav-dark-link-color-hover);
}
}
&.current {
color: var(--color-nav-current-link);
cursor: default;
&:hover {
color: var(--color-nav-current-link);
}
@include nav-dark {
color: var(--color-nav-dark-current-link);
&:hover {
color: var(--color-nav-dark-current-link);
}
}
}
}
</style>