From b81895af7ec602ea7beb888a9be82f54bbc5a693 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:53:33 -0400 Subject: [PATCH] theme dx --- packages/sdk/docs-samples/tsconfig.json | 7 ++ packages/sdk/vite.mjs | 4 + packages/sdk/vite.spec.ts | 20 ++++ .../admin/themes/workbench/ViewPreview.tsx | 18 +++- resources/scripts/extensions/Slot.tsx | 3 + resources/scripts/router/viewRoutes.tsx | 7 +- resources/scripts/views/View.tsx | 15 +-- resources/scripts/views/slotCheck.spec.tsx | 95 +++++++++++++++++++ resources/scripts/views/slotCheck.tsx | 58 +++++++++++ 9 files changed, 215 insertions(+), 12 deletions(-) create mode 100644 packages/sdk/docs-samples/tsconfig.json create mode 100644 resources/scripts/views/slotCheck.spec.tsx create mode 100644 resources/scripts/views/slotCheck.tsx diff --git a/packages/sdk/docs-samples/tsconfig.json b/packages/sdk/docs-samples/tsconfig.json new file mode 100644 index 000000000..6c152a2ea --- /dev/null +++ b/packages/sdk/docs-samples/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "target": "ES2022", "module": "ESNext", "moduleResolution": "bundler", "jsx": "react-jsx", + "lib": ["ES2022", "DOM", "DOM.Iterable"], "types": [], "strict": true, "noEmit": true, "skipLibCheck": true + }, + "include": ["**/*.ts", "**/*.tsx"] +} diff --git a/packages/sdk/vite.mjs b/packages/sdk/vite.mjs index d4a1bf30c..67e71c08d 100644 --- a/packages/sdk/vite.mjs +++ b/packages/sdk/vite.mjs @@ -153,6 +153,8 @@ export function defineThemeConfig({ entry, stylesheet, outDir = 'dist', plugins plugins: [...plugins, themeBuildInfo()], // The import map shares react/jsx-runtime, never the development runtime. oxc: { jsx: { runtime: 'automatic', development: false } }, + // The panel serves a build from its own directory, so fonts and images resolve beside the file using them. + base: './', publicDir: false, build: { outDir, @@ -183,6 +185,8 @@ export function defineExtensionConfig({ entry, outDir = 'dist', plugins = [] } = return { plugins: [...plugins, extensionStyles()], + // The panel serves a build from its own directory, so fonts and images resolve beside the file using them. + base: './', publicDir: false, build: { outDir, diff --git a/packages/sdk/vite.spec.ts b/packages/sdk/vite.spec.ts index 91b5b9fd9..42b4379da 100644 --- a/packages/sdk/vite.spec.ts +++ b/packages/sdk/vite.spec.ts @@ -73,6 +73,26 @@ it('leaves out views it cannot read without running the theme, and builds a styl } }); +it('ships fonts beside the theme stylesheet, linked relative to it', async () => { + const directory = mkdtempSync(resolve(tmpdir(), 'theme-build-')); + try { + // Over Vite's inline limit, so the font is emitted as a file rather than a data URL. + writeFileSync(resolve(directory, 'Brand.woff2'), Buffer.alloc(8192, 1)); + const dist = await buildTheme( + directory, + { + 'theme.css': + "@font-face { font-family: 'Brand'; src: url('./Brand.woff2') format('woff2'); }\n.a { font-family: Brand; }", + }, + false + ); + + expect(dist('theme.css')).toMatch(/url\(\.\/Brand\.[\w-]+\.woff2\)/); + } finally { + rmSync(directory, { recursive: true, force: true }); + } +}); + it('refuses theme JavaScript that imports styles the panel would never load', async () => { const directory = mkdtempSync(resolve(tmpdir(), 'theme-build-')); try { diff --git a/resources/scripts/components/admin/themes/workbench/ViewPreview.tsx b/resources/scripts/components/admin/themes/workbench/ViewPreview.tsx index 473cb84e6..9d8084e23 100644 --- a/resources/scripts/components/admin/themes/workbench/ViewPreview.tsx +++ b/resources/scripts/components/admin/themes/workbench/ViewPreview.tsx @@ -10,7 +10,9 @@ import { ViewInspectionContext, type ViewInspection, type ViewProbe } from '@/vi import { modelJson } from '@/views/inspector/model'; import { CopyButton, LayerStack, ModelTree, OverrideHelp, Section, SlotList } from '@/views/inspector/parts'; import { renderingLayer } from '@/views/inspector/snippets'; +import { OPAQUE_PAGES, pageSlots, VIEW_PAGES, type PageViewName } from '@/views/pages'; import { getViewChain, getViewDefinition, useViewDefinitions, type ViewDefinition } from '@/views/registry'; +import { EngineSlots } from '@/views/slotCheck'; import type { ThemeScheme } from '@/views/theme'; import { LooseView, ViewFixtureProvider } from '@/views/View'; import FixtureBoundary from './FixtureBoundary'; @@ -19,6 +21,9 @@ import { frameSize, useWidth } from './stage'; export const ALL_FIXTURES = '*'; +const isPageViewName = (name: string): name is PageViewName => + Object.hasOwn(VIEW_PAGES, name) || Object.hasOwn(OPAQUE_PAGES, name); + type NestedFixtures = Readonly>; interface FrameOptions { @@ -72,6 +77,7 @@ function FixtureFrame({ ); // Pages and layouts bring their own spacing; a block sits in some, as it would on a page. const view = definition.kind === 'block' ?
{rendered}
: rendered; + const engineSlots = isPageViewName(definition.name) ? (pageSlots(definition.name) ?? []) : []; return (
@@ -102,11 +108,13 @@ function FixtureFrame({ }> - {definition.context === 'server' ? ( - {view} - ) : ( - view - )} + + {definition.context === 'server' ? ( + {view} + ) : ( + view + )} + diff --git a/resources/scripts/extensions/Slot.tsx b/resources/scripts/extensions/Slot.tsx index cbfd7b464..9c6375b1a 100644 --- a/resources/scripts/extensions/Slot.tsx +++ b/resources/scripts/extensions/Slot.tsx @@ -1,5 +1,6 @@ import { getSlotComponents, useExtensionRegistry, type SlotProps } from '@/extensions/registry'; import ExtensionMount from '@/extensions/ExtensionMount'; +import { useRenderedSlot } from '@/views/slotCheck'; function slotResetKey(name: SlotProps['name'], data: SlotProps['data']): string { if (!data) { @@ -29,6 +30,8 @@ export default function Slot({ name, data }: SlotProps) { const registrations = useExtensionRegistry(() => getSlotComponents(name)); const resetKey = slotResetKey(name, data); + useRenderedSlot(name); + return ( <> {registrations.map(({ id, extensionId, component: Component }) => ( diff --git a/resources/scripts/router/viewRoutes.tsx b/resources/scripts/router/viewRoutes.tsx index a92c9172a..e6879d259 100644 --- a/resources/scripts/router/viewRoutes.tsx +++ b/resources/scripts/router/viewRoutes.tsx @@ -9,9 +9,12 @@ import { useRouteSlotData } from '@/router/routeSlots'; import { LayoutMainCheck } from '@/views/a11y'; import { pageSlots, type PageViewName } from '@/views/pages'; import { ViewSessionProvider } from '@/views/session'; +import { EngineSlots } from '@/views/slotCheck'; import { preloadView, View, ViewArea } from '@/views/View'; import type { ViewContext } from '@/views/registry'; +const noSlots: readonly string[] = []; + export type LayoutViewName = 'layout.auth' | 'layout.app' | 'layout.account' | 'layout.server' | 'layout.admin'; /** A server tab `beforeLoad` matching PermissionRoute's any-of check. */ @@ -70,7 +73,9 @@ export function pageRoute( {slots && } - {Frame ? {page} : page} + + {Frame ? {page} : page} + {slots && } diff --git a/resources/scripts/views/View.tsx b/resources/scripts/views/View.tsx index 4c3e7189d..0dcab36a0 100644 --- a/resources/scripts/views/View.tsx +++ b/resources/scripts/views/View.tsx @@ -31,6 +31,7 @@ import { type ViewProps, } from './registry'; import { useViewDecision } from './session'; +import { SlotCheck } from './slotCheck'; const ViewAreaContext = createContext([]); @@ -293,12 +294,14 @@ function ViewBody({ definition, model, inputs, className, children }: BodyProps } const chain = ( - + + + ); return inspection ? ( diff --git a/resources/scripts/views/slotCheck.spec.tsx b/resources/scripts/views/slotCheck.spec.tsx new file mode 100644 index 000000000..812e15e39 --- /dev/null +++ b/resources/scripts/views/slotCheck.spec.tsx @@ -0,0 +1,95 @@ +/** @vitest-environment jsdom */ +import { act, cleanup, render } from '@testing-library/react'; +import type { ComponentType } from 'react'; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; +import Slot from '@/extensions/Slot'; +import type { SlotName } from '@/extensions/registry'; +import { clearThemeLayers, registerViewDefinition, registerViewLayer, type ViewProps } from './registry'; +import { EngineSlots, SLOT_CHECK_DELAY_MS } from './slotCheck'; +import { View } from './View'; + +const before = 'server.console.power.before' satisfies SlotName; +const after = 'server.console.power.after' satisfies SlotName; +type Props = ViewProps<'test.slotted'>; + +beforeAll(() => { + registerViewDefinition({ + name: 'test.slotted', + kind: 'block', + context: 'app', + use: () => ({}), + slots: [before, after], + }); + registerViewLayer('classic', 'classic', { + 'test.slotted': () => ( + <> + +

classic

+ + + ), + }); +}); + +let warn: ReturnType; + +beforeEach(() => { + vi.useFakeTimers(); + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); +}); + +afterEach(() => { + cleanup(); + clearThemeLayers(); + vi.useRealTimers(); + vi.restoreAllMocks(); +}); + +async function renderWith(layer: ComponentType, engine: readonly string[] = []) { + registerViewLayer('aurora', 'theme', { 'test.slotted': layer }); + render( + + + + ); + await act(async () => { + vi.advanceTimersByTime(SLOT_CHECK_DELAY_MS); + }); +} + +describe('slot check', () => { + it('warns when a layer renders none of its view slots', async () => { + await renderWith(() =>

aurora

); + + expect(warn).toHaveBeenCalledWith( + `[views] The "aurora" layer of "test.slotted" renders none of its slots (${before}, ${after}). ` + + 'Render them with , or render , so extensions keep their place.' + ); + }); + + it('stays quiet for a layer that renders a slot or the layer below', async () => { + await renderWith(() => ); + cleanup(); + await renderWith(({ Default }) => ( +
+ +
+ )); + + expect(warn).not.toHaveBeenCalled(); + }); + + it('leaves out the slots the engine renders around a page', async () => { + await renderWith(() =>

aurora

, [before, after]); + + expect(warn).not.toHaveBeenCalled(); + }); + + it('does not count the slots of a nested view', async () => { + registerViewDefinition({ name: 'test.nested', kind: 'block', context: 'app', use: () => ({}) }); + registerViewLayer('classic', 'classic', { 'test.nested': () => }); + await renderWith(() => ); + + expect(warn).toHaveBeenCalledOnce(); + }); +}); diff --git a/resources/scripts/views/slotCheck.tsx b/resources/scripts/views/slotCheck.tsx new file mode 100644 index 000000000..48f68dc04 --- /dev/null +++ b/resources/scripts/views/slotCheck.tsx @@ -0,0 +1,58 @@ +import { createContext, useContext, useEffect, useRef, useState, type ReactNode } from 'react'; +import type { ViewDefinition, ViewLayer } from './registry'; + +const SlotRecorder = createContext<((name: string) => void) | null>(null); +/** The slots the engine renders around a page view, which the view itself does not render. */ +export const EngineSlots = createContext([]); + +/** Tells the view a slot renders in that it did. */ +export function useRenderedSlot(name: string): void { + const record = useContext(SlotRecorder); + + useEffect(() => record?.(name), [record, name]); +} + +export const SLOT_CHECK_DELAY_MS = 3000; + +/** + * In development, warns when a theme or extension layer renders none of the slots its view renders, + * which hides whatever extensions mount there. Rendering `Default` renders the slots of the layers below. + */ +export function SlotCheck({ + definition, + layer, + children, +}: { + definition: ViewDefinition; + layer: ViewLayer | undefined; + children?: ReactNode; +}) { + const engine = useContext(EngineSlots); + const rendered = useRef(false); + const [record] = useState(() => () => { + rendered.current = true; + }); + const slots = (definition.slots ?? []).filter((slot) => !engine.includes(slot)); + const owner = import.meta.env.DEV && slots.length > 0 && layer?.source !== 'classic' ? layer?.id : undefined; + const expected = slots.join(', '); + + useEffect(() => { + if (owner === undefined) { + return; + } + + // Lazy layers and their nested views render after this effect. + const timer = setTimeout(() => { + if (!rendered.current) { + console.warn( + `[views] The "${owner}" layer of "${definition.name}" renders none of its slots (${expected}). ` + + 'Render them with , or render , so extensions keep their place.' + ); + } + }, SLOT_CHECK_DELAY_MS); + + return () => clearTimeout(timer); + }, [owner, definition.name, expected]); + + return {children}; +}