Skip to content

Commit

Permalink
Fixed replacement of skip link and announcement bar on instant load
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Mar 28, 2020
1 parent edc9d6f commit 908d34b
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 58 deletions.
2 changes: 2 additions & 0 deletions material/assets/javascripts/bundle.0ddca959.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions material/assets/javascripts/bundle.0ddca959.min.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions material/assets/javascripts/bundle.edc2ff56.min.js

This file was deleted.

1 change: 0 additions & 1 deletion material/assets/javascripts/bundle.edc2ff56.min.js.map

This file was deleted.

6 changes: 3 additions & 3 deletions material/assets/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"assets/javascripts/bundle.js": "assets/javascripts/bundle.edc2ff56.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.edc2ff56.min.js.map",
"assets/javascripts/bundle.js": "assets/javascripts/bundle.0ddca959.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.0ddca959.min.js.map",
"assets/javascripts/vendor.js": "assets/javascripts/vendor.c1fcc1cc.min.js",
"assets/javascripts/vendor.js.map": "assets/javascripts/vendor.c1fcc1cc.min.js.map",
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.3bc815f0.min.js",
"assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.3bc815f0.min.js.map",
"assets/stylesheets/main.css": "assets/stylesheets/main.b32d3181.min.css",
"assets/stylesheets/main.css": "assets/stylesheets/main.c1451e9e.min.css",
"assets/stylesheets/palette.css": "assets/stylesheets/palette.4444686e.min.css"
}
1 change: 0 additions & 1 deletion material/assets/stylesheets/main.b32d3181.min.css

This file was deleted.

1 change: 1 addition & 0 deletions material/assets/stylesheets/main.c1451e9e.min.css

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions material/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{% endif %}
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{{ 'assets/stylesheets/main.b32d3181.min.css' | url }}">
<link rel="stylesheet" href="{{ 'assets/stylesheets/main.c1451e9e.min.css' | url }}">
{% if palette.primary or palette.accent %}
<link rel="stylesheet" href="{{ 'assets/stylesheets/palette.4444686e.min.css' | url }}">
{% endif %}
Expand Down Expand Up @@ -88,19 +88,23 @@
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" for="__drawer"></label>
{% if page.toc | first is defined %}
{% set skip = page.toc | first %}
<a href="{{ skip.url | url }}" class="md-skip" data-md-component="skip">
{{ lang.t('skip.link.title') }}
</a>
{% endif %}
{% if self.announce() %}
<aside class="md-announce" data-md-component="announce">
<div class="md-announce__inner md-grid md-typeset">
{% block announce %}{% endblock %}
</div>
</aside>
{% endif %}
<div data-md-component="skip">
{% if page.toc | first is defined %}
{% set skip = page.toc | first %}
<a href="{{ skip.url | url }}" class="md-skip">
{{ lang.t('skip.link.title') }}
</a>
{% endif %}
</div>
<div data-md-component="announce">
{% if self.announce() %}
<aside class="md-announce">
<div class="md-announce__inner md-grid md-typeset">
{% block announce %}{% endblock %}
</div>
</aside>
{% endif %}
</div>
{% block header %}
{% include "partials/header.html" %}
{% endblock %}
Expand Down Expand Up @@ -175,7 +179,7 @@ <h1>{{ page.title | default(config.site_name, true)}}</h1>
</div>
{% block scripts %}
<script src="{{ 'assets/javascripts/vendor.c1fcc1cc.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.edc2ff56.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.0ddca959.min.js' | url }}"></script>
{%- set translations = {} -%}
{%- for key in [
"clipboard.copy",
Expand Down
2 changes: 2 additions & 0 deletions src/assets/javascripts/components/_/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { getElement, replaceElement } from "browser"
* Component
*/
export type Component =
| "announce" /* Announcement bar */
| "container" /* Container */
| "header" /* Header */
| "header-title" /* Header title */
Expand Down Expand Up @@ -114,6 +115,7 @@ export function setupComponents(
switch (name) {

/* Top-level components: update */
case "announce":
case "header-title":
case "container":
case "skip":
Expand Down
5 changes: 3 additions & 2 deletions src/assets/javascripts/components/header/_/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface Header {
* Mount options
*/
interface MountOptions {
document$: Observable<Document> /* Document observable */
viewport$: Observable<Viewport> /* Viewport observable */
}

Expand All @@ -88,11 +89,11 @@ interface MountOptions {
* @return Operator function
*/
export function mountHeader(
{ viewport$ }: MountOptions
{ document$, viewport$ }: MountOptions
): OperatorFunction<HTMLElement, Header> {
return pipe(
switchMap(el => {
const header$ = watchHeader(el)
const header$ = watchHeader(el, { document$ })

/* Compute whether the header should switch to page header */
const type$ = useComponent("main")
Expand Down
62 changes: 42 additions & 20 deletions src/assets/javascripts/components/header/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import {
pipe
} from "rxjs"
import {
distinctUntilChanged,
finalize,
map,
observeOn,
shareReplay,
switchMap,
tap
} from "rxjs/operators"

Expand All @@ -43,6 +45,17 @@ import {
setHeaderTitleActive
} from "../set"

/* ----------------------------------------------------------------------------
* Helper types
* ------------------------------------------------------------------------- */

/**
* Watch options
*/
interface WatchOptions {
document$: Observable<Document> /* Document observable */
}

/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
Expand All @@ -55,27 +68,36 @@ import {
* @return Header observable
*/
export function watchHeader(
el: HTMLElement
el: HTMLElement, { document$ }: WatchOptions
): Observable<Omit<Header, "type">> {
const styles = getComputedStyle(el)
if ([
"sticky", /* Modern browsers */
"-webkit-sticky" /* Safari */
].includes(styles.position)) {
return watchElementSize(el)
.pipe(
map(({ height }) => ({
sticky: true,
height
})),
shareReplay(1)
)
} else {
return of({
sticky: false,
height: 0
})
}
return document$
.pipe(
map(() => {
const styles = getComputedStyle(el)
return [
"sticky", /* Modern browsers */
"-webkit-sticky" /* Safari */
].includes(styles.position)
}),
distinctUntilChanged(),
switchMap(sticky => {
if (sticky) {
return watchElementSize(el)
.pipe(
map(({ height }) => ({
sticky: true,
height
}))
)
} else {
return of({
sticky: false,
height: 0
})
}
}),
shareReplay(1)
)
}

/* ------------------------------------------------------------------------- */
Expand Down
3 changes: 2 additions & 1 deletion src/assets/javascripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export function initialize(config: unknown) {

/* Set up component bindings */
setupComponents([
"announce", /* Announcement bar */
"container", /* Container */
"header", /* Header */
"header-title", /* Header title */
Expand Down Expand Up @@ -192,7 +193,7 @@ export function initialize(config: unknown) {
/* Create header observable */
const header$ = useComponent("header")
.pipe(
mountHeader({ viewport$ }),
mountHeader({ document$, viewport$ }),
shareReplay(1)
)

Expand Down
1 change: 1 addition & 0 deletions src/assets/stylesheets/layout/_announce.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

// Announcement bar
.md-announce {
overflow: auto;
background-color: var(--md-default-fg-color);

// Actual content
Expand Down
30 changes: 17 additions & 13 deletions src/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,25 @@
<label class="md-overlay" for="__drawer"></label>

<!-- Link to skip to content -->
{% if page.toc | first is defined %}
{% set skip = page.toc | first %}
<a href="{{ skip.url | url }}" class="md-skip" data-md-component="skip">
{{ lang.t('skip.link.title') }}
</a>
{% endif %}
<div data-md-component="skip">
{% if page.toc | first is defined %}
{% set skip = page.toc | first %}
<a href="{{ skip.url | url }}" class="md-skip">
{{ lang.t('skip.link.title') }}
</a>
{% endif %}
</div>

<!-- Announcement bar -->
{% if self.announce() %}
<aside class="md-announce" data-md-component="announce">
<div class="md-announce__inner md-grid md-typeset">
{% block announce %}{% endblock %}
</div>
</aside>
{% endif %}
<div data-md-component="announce">
{% if self.announce() %}
<aside class="md-announce">
<div class="md-announce__inner md-grid md-typeset">
{% block announce %}{% endblock %}
</div>
</aside>
{% endif %}
</div>

<!-- Application header -->
{% block header %}
Expand Down

0 comments on commit 908d34b

Please sign in to comment.