Skip to content

Commit

Permalink
Refactored theme-color implementation to support palette switches
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Dec 11, 2022
1 parent 1408c29 commit 944180d
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 144 deletions.
1 change: 0 additions & 1 deletion docs/customization.md
Expand Up @@ -113,7 +113,6 @@ assets may also be put in the `overrides` directory:
│ ├─ nav.html # Main navigation
│ ├─ nav-item.html # Main navigation item
│ ├─ pagination.html # Pagination (used for blog)
│ ├─ palette.html # Color palette
│ ├─ post.html # Blog post excerpt
│ ├─ search.html # Search interface
│ ├─ social.html # Social links
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion material/assets/stylesheets/extra.27126f53.min.css

This file was deleted.

1 change: 0 additions & 1 deletion material/assets/stylesheets/extra.27126f53.min.css.map

This file was deleted.

1 change: 1 addition & 0 deletions material/assets/stylesheets/extra.73715943.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions material/assets/stylesheets/extra.73715943.min.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 6 additions & 11 deletions material/base.html
Expand Up @@ -38,13 +38,6 @@
{% if config.theme.palette %}
{% set palette = config.theme.palette %}
<link rel="stylesheet" href="{{ 'assets/stylesheets/palette.2505c338.min.css' | url }}">
{% if palette.primary %}
{% import "partials/palette.html" as map %}
{% set primary = map.primary(
palette.primary | replace(" ", "-") | lower
) %}
<meta name="theme-color" content="{{ primary }}">
{% endif %}
{% endif %}
{% include "partials/icons.html" %}
{% endblock %}
Expand Down Expand Up @@ -81,8 +74,10 @@
{% if not palette is mapping %}
{% set palette = palette | first %}
{% endif %}
{% set scheme = palette.scheme | d("default", true) %}
<body dir="{{ direction }}" data-md-color-scheme="{{ scheme | replace(' ', '-') }}" data-md-color-primary="{{ palette.primary | replace(' ', '-') }}" data-md-color-accent="{{ palette.accent | replace(' ', '-') }}">
{% set scheme = palette.scheme | d("default", true) %}
{% set primary = palette.primary | d("", true) %}
{% set accent = palette.accent | d("", true) %}
<body dir="{{ direction }}" data-md-color-scheme="{{ scheme | replace(' ', '-') }}" data-md-color-primary="{{ primary | replace(' ', '-') }}" data-md-color-accent="{{ accent | replace(' ', '-') }}">
{% else %}
<body dir="{{ direction }}">
{% endif %}
Expand Down Expand Up @@ -239,13 +234,13 @@
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.ce0331ff.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.c0285e69.min.js' | url }}"></script>
{% for path in config.extra_javascript %}
<script src="{{ path | url }}"></script>
{% endfor %}
{% endblock %}
{% if page.meta and page.meta.ᴴₒᴴₒᴴₒ %}
<link rel="stylesheet" href="{{ 'assets/stylesheets/extra.27126f53.min.css' | url }}">
<link rel="stylesheet" href="{{ 'assets/stylesheets/extra.73715943.min.css' | url }}">
<script src="{{ 'assets/javascripts/extra/bundle.f719a234.min.js' | url }}" defer></script>
{% endif %}
</body>
Expand Down
44 changes: 0 additions & 44 deletions material/partials/palette.html

This file was deleted.

27 changes: 26 additions & 1 deletion src/assets/javascripts/components/palette/index.ts
Expand Up @@ -37,8 +37,12 @@ import {
} from "rxjs"

import { getElements } from "~/browser"
import { h } from "~/utilities"

import { Component } from "../_"
import {
Component,
getComponentElement
} from "../_"

/* ----------------------------------------------------------------------------
* Types
Expand Down Expand Up @@ -112,6 +116,10 @@ export function watchPalette(
export function mountPalette(
el: HTMLElement
): Observable<Component<Palette>> {
const meta = h("meta", { name: "theme-color" })
document.head.appendChild(meta)

/* Mount component on subscription */
return defer(() => {
const push$ = new Subject<Palette>()
push$.subscribe(palette => {
Expand All @@ -132,6 +140,23 @@ export function mountPalette(
__md_set("__palette", palette)
})

/* Update theme-color meta tag */
push$
.pipe(
map(() => {
const header = getComponentElement("header")
const { backgroundColor } = window.getComputedStyle(header)

/* Return color in hexadecimal format */
return `#${
backgroundColor.match(/\d+/g)!
.map(value => (+value).toString(16).padStart(2, "0"))
.join("")
}`
})
)
.subscribe(color => meta.content = color)

/* Revert transition durations after color switch */
push$.pipe(observeOn(asyncScheduler))
.subscribe(() => {
Expand Down
17 changes: 5 additions & 12 deletions src/base.html
Expand Up @@ -82,15 +82,6 @@
rel="stylesheet"
href="{{ 'assets/stylesheets/palette.css' | url }}"
/>

<!-- Theme-color meta tag for Android -->
{% if palette.primary %}
{% import "partials/palette.html" as map %}
{% set primary = map.primary(
palette.primary | replace(" ", "-") | lower
) %}
<meta name="theme-color" content="{{ primary }}" />
{% endif %}
{% endif %}

<!-- Custom icons -->
Expand Down Expand Up @@ -159,12 +150,14 @@
{% if not palette is mapping %}
{% set palette = palette | first %}
{% endif %}
{% set scheme = palette.scheme | d("default", true) %}
{% set scheme = palette.scheme | d("default", true) %}
{% set primary = palette.primary | d("", true) %}
{% set accent = palette.accent | d("", true) %}
<body
dir="{{ direction }}"
data-md-color-scheme="{{ scheme | replace(' ', '-') }}"
data-md-color-primary="{{ palette.primary | replace(' ', '-') }}"
data-md-color-accent="{{ palette.accent | replace(' ', '-') }}"
data-md-color-primary="{{ primary | replace(' ', '-') }}"
data-md-color-accent="{{ accent | replace(' ', '-') }}"
>
{% else %}
<body dir="{{ direction }}">
Expand Down

0 comments on commit 944180d

Please sign in to comment.