Refactored theme-color implementation to support palette switches

This commit is contained in:
squidfunk 2022-12-11 17:43:09 +01:00
parent 1408c29ba9
commit 944180d572
12 changed files with 46 additions and 144 deletions

View File

@ -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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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 %}
@ -82,7 +75,9 @@
{% 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 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 %}
@ -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>

View File

@ -1,44 +0,0 @@
{#-
This file was automatically generated - do not edit
-#}
{% macro primary(key) %}{{ {
"red": "#ef5552",
"pink": "#e92063",
"purple": "#ab47bd",
"deep-purple": "#7e56c2",
"indigo": "#4051b5",
"blue": "#2094f3",
"light-blue": "#02a6f2",
"cyan": "#00bdd6",
"teal": "#009485",
"green": "#4cae4f",
"light-green": "#8bc34b",
"lime": "#cbdc38",
"yellow": "#ffec3d",
"amber": "#ffc105",
"orange": "#ffa724",
"deep-orange": "#ff6e42",
"brown": "#795649",
"grey": "#757575",
"blue-grey": "#546d78",
"black": "#000000",
"white": "#ffffff"
}[key] }}{% endmacro %}
{% macro accent(key) %}{{ {
"red": "#ff1a47",
"pink": "#f50056",
"purple": "#df41fb",
"deep-purple": "#7c4dff",
"indigo": "#526cfe",
"blue": "#4287ff",
"light-blue": "#0091eb",
"cyan": "#00bad6",
"teal": "#00bda4",
"green": "#00c753",
"light-green": "#63de17",
"lime": "#b0eb00",
"yellow": "#ffd500",
"amber": "#ffaa00",
"orange": "#ff9100",
"deep-orange": "#ff6e42"
}[key] }}{% endmacro %}

View File

@ -37,8 +37,12 @@ import {
} from "rxjs"
import { getElements } from "~/browser"
import { h } from "~/utilities"
import { Component } from "../_"
import {
Component,
getComponentElement
} from "../_"
/* ----------------------------------------------------------------------------
* Types
@ -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 => {
@ -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(() => {

View File

@ -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 -->
@ -160,11 +151,13 @@
{% set palette = palette | first %}
{% endif %}
{% 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 }}">

View File

@ -1,66 +0,0 @@
<!--
Copyright (c) 2016-2022 Martin Donath <martin.donath@squidfunk.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->
<!-- Primary colors -->
{% macro primary(key) %}{{ {
"red": "#ef5552",
"pink": "#e92063",
"purple": "#ab47bd",
"deep-purple": "#7e56c2",
"indigo": "#4051b5",
"blue": "#2094f3",
"light-blue": "#02a6f2",
"cyan": "#00bdd6",
"teal": "#009485",
"green": "#4cae4f",
"light-green": "#8bc34b",
"lime": "#cbdc38",
"yellow": "#ffec3d",
"amber": "#ffc105",
"orange": "#ffa724",
"deep-orange": "#ff6e42",
"brown": "#795649",
"grey": "#757575",
"blue-grey": "#546d78",
"black": "#000000",
"white": "#ffffff"
}[key] }}{% endmacro %}
<!-- Accent colors -->
{% macro accent(key) %}{{ {
"red": "#ff1a47",
"pink": "#f50056",
"purple": "#df41fb",
"deep-purple": "#7c4dff",
"indigo": "#526cfe",
"blue": "#4287ff",
"light-blue": "#0091eb",
"cyan": "#00bad6",
"teal": "#00bda4",
"green": "#00c753",
"light-green": "#63de17",
"lime": "#b0eb00",
"yellow": "#ffd500",
"amber": "#ffaa00",
"orange": "#ff9100",
"deep-orange": "#ff6e42"
}[key] }}{% endmacro %}