Skip to content

Build your personal knowledge base with Trilium Notes

client/icon pack/preview: get access to CSS variables from the current theme

Adorian Doran1e2760458457
diff --git a/apps/client/src/widgets/react/IsolatedFrame.tsx b/apps/client/src/widgets/react/IsolatedFrame.tsx
index 1e22fdffba..1e29c2cd78 100644
--- a/apps/client/src/widgets/react/IsolatedFrame.tsx
+++ b/apps/client/src/widgets/react/IsolatedFrame.tsx
@@ -2,12 +2,16 @@ import { ComponentChildren } from "preact";
import { createPortal } from "preact/compat";
import { useLayoutEffect, useRef, useState } from "preact/hooks";
+import { useColorScheme } from "./hooks";
+
interface IsolatedFrameProps {
className?: string;
/** Accessible title for the underlying `<iframe>`. */
title: string;
/** CSS injected into the isolated document's `<head>`. Kept in sync on change. */
css?: string;
+ /** Host `:root` CSS custom properties to mirror into the frame, so its CSS can use `var(...)`. Re-synced on theme change. */
+ cssVars?: string[];
children?: ComponentChildren;
}
@@ -17,12 +21,14 @@ interface IsolatedFrameProps {
* and nothing rendered here leaks out. Children are Preact nodes portalled into the frame's `<body>`,
* so they stay live and interactive (re-render on prop changes) rather than being a static snapshot.
*
- * Provide the frame's own styling via {@link IsolatedFrameProps.css}. Because the document has no
- * base URL, any URLs referenced from that CSS must be absolute.
+ * Provide the frame's own styling via {@link IsolatedFrameProps.css}; theme values can be forwarded
+ * with {@link IsolatedFrameProps.cssVars}. Because the document has no base URL, any URLs referenced
+ * from that CSS must be absolute.
*/
-export default function IsolatedFrame({ className, title, css, children }: IsolatedFrameProps) {
+export default function IsolatedFrame({ className, title, css, cssVars, children }: IsolatedFrameProps) {
const iframeRef = useRef<HTMLIFrameElement>(null);
const [ body, setBody ] = useState<HTMLElement | null>(null);
+ const colorScheme = useColorScheme();
// A same-origin, src-less iframe exposes an empty document synchronously once mounted; re-capture
// on load in case the browser swaps the initial about:blank document.
@@ -32,6 +38,7 @@ export default function IsolatedFrame({ className, title, css, children }: Isola
};
useLayoutEffect(captureBody, []);
+ // `colorScheme` is not read directly — it re-runs this effect so mirrored vars pick up the new theme.
useLayoutEffect(() => {
const doc = body?.ownerDocument;
if (!doc) return;
@@ -41,8 +48,8 @@ export default function IsolatedFrame({ className, title, css, children }: Isola
style.id = "isolated-frame-style";
doc.head.appendChild(style);
}
- style.textContent = css ?? "";
- }, [ css, body ]);
+ style.textContent = `${buildRootVars(cssVars)}${css ?? ""}`;
+ }, [ css, cssVars, colorScheme, body ]);
return (
<>
@@ -51,3 +58,15 @@ export default function IsolatedFrame({ className, title, css, children }: Isola
</>
);
}
+
+/** Reads the named custom properties off the host's `:root` and emits a `:root { ... }` block for the frame. */
+function buildRootVars(names: string[] | undefined): string {
+ if (!names?.length) return "";
+ const hostStyle = getComputedStyle(document.documentElement);
+ const declarations = names
+ .map((name) => [ name, hostStyle.getPropertyValue(name).trim() ] as const)
+ .filter(([ , value ]) => value)
+ .map(([ name, value ]) => `${name}: ${value};`)
+ .join(" ");
+ return declarations ? `:root { ${declarations} }\n` : "";
+}
diff --git a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx
index 177520cf70..9e82e95c6f 100644
--- a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx
+++ b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx
@@ -5,7 +5,7 @@ import { useEffect, useMemo, useState } from "preact/hooks";
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
import { isDesktop } from "../../../services/utils";
-import { useColorScheme, useNoteBlob, useNoteLabel } from "../../react/hooks";
+import { useNoteBlob, useNoteLabel } from "../../react/hooks";
import IsolatedFrame from "../../react/IsolatedFrame";
import NoItems from "../../react/NoItems";
import { TextPreview } from "../File";
@@ -15,6 +15,16 @@ import { TypeWidgetProps } from "../type_widget";
/** How long to wait after the last edit before re-parsing and re-rendering the (potentially large) preview. */
const PREVIEW_DEBOUNCE_MS = 1500;
+/** Theme variables forwarded into the isolated preview frame so it matches the app's colours. */
+const PREVIEW_CSS_VARS = [
+ "--main-text-color",
+ "--main-background-color",
+ "--muted-text-color",
+ "--main-border-color",
+ "--accented-background-color",
+ "--hover-item-background-color"
+];
+
export default function IconPack(props: TypeWidgetProps) {
const [ content, setContent ] = useState("");
// Icon packs shipped in distributable zips are `file` notes, whose content isn't editable as
@@ -47,16 +57,12 @@ function FileSource({ note }: { note: FNote }) {
function IconPackPreview({ note, content }: { note: FNote; content: string }) {
const [ prefix ] = useNoteLabel(note, "iconPack");
- // Recompute the injected theme colors when the app switches between light and dark.
- const colorScheme = useColorScheme();
// The user isn't meant to see the preview update on every keystroke — it lags a beat behind.
const debounced = useDebouncedValue(content, PREVIEW_DEBOUNCE_MS);
const font = useIconPackFont(note);
const parsed = useMemo(() => parseManifest(debounced), [ debounced ]);
- // `getThemeColors()` reads theme-dependent CSS variables; `colorScheme` is the recompute trigger.
- // eslint-disable-next-line react-hooks/exhaustive-deps
- const css = useMemo(() => buildPreviewCss(font, getThemeColors()), [ font, colorScheme ]);
+ const css = useMemo(() => buildPreviewCss(font), [ font ]);
if (!parsed.ok) {
return <div className="icon-pack-preview"><NoItems icon="bx bx-error-circle" text={t("icon_pack.invalid_manifest")} /></div>;
@@ -71,7 +77,7 @@ function IconPackPreview({ note, content }: { note: FNote; content: string }) {
}
return (
- <IsolatedFrame className="icon-pack-frame" title={t("icon_pack.preview_title")} css={css}>
+ <IsolatedFrame className="icon-pack-frame" title={t("icon_pack.preview_title")} css={css} cssVars={PREVIEW_CSS_VARS}>
<div className="ip-grid">
{parsed.icons.map((icon) => (
<div className="ip-cell" key={icon.id} title={iconTooltip(prefix, icon)}>
@@ -173,15 +179,7 @@ function useIconPackFont(note: FNote): IconPackFont | null {
const PREVIEW_FONT_FAMILY = "tn-icon-pack-preview";
-function getThemeColors() {
- const style = getComputedStyle(document.documentElement);
- return {
- text: style.getPropertyValue("--main-text-color").trim() || "#000",
- background: style.getPropertyValue("--main-background-color").trim() || "#fff"
- };
-}
-
-function buildPreviewCss(font: IconPackFont | null, colors: { text: string; background: string }): string {
+function buildPreviewCss(font: IconPackFont | null): string {
const fontFace = font
? `@font-face { font-family: "${PREVIEW_FONT_FAMILY}"; src: url("${font.url}") format("${font.format}"); font-display: block; }`
: "";
@@ -189,8 +187,8 @@ function buildPreviewCss(font: IconPackFont | null, colors: { text: string; back
${fontFace}
html, body { margin: 0; height: 100%; }
body {
- color: ${colors.text};
- background: ${colors.background};
+ color: var(--main-text-color, #000);
+ background: var(--main-background-color, #fff);
padding: 8px;
box-sizing: border-box;
overflow: auto;
@@ -214,7 +212,7 @@ function buildPreviewCss(font: IconPackFont | null, colors: { text: string; back
content-visibility: auto;
contain-intrinsic-size: 32px 32px;
}
- .ip-cell:hover { background: rgba(127, 127, 127, 0.2); }
+ .ip-cell:hover { background: var(--hover-item-background-color, rgba(127, 127, 127, 0.2)); }
.ip-glyph { font-family: "${PREVIEW_FONT_FAMILY}"; font-size: 32px; line-height: 1; }
`;
}