mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 04:15:52 -05:00
theme dx
This commit is contained in:
@@ -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"]
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<Record<string, string>>;
|
||||
|
||||
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' ? <div className='p-4'>{rendered}</div> : rendered;
|
||||
const engineSlots = isPageViewName(definition.name) ? (pageSlots(definition.name) ?? []) : [];
|
||||
|
||||
return (
|
||||
<figure className='min-w-0'>
|
||||
@@ -102,11 +108,13 @@ function FixtureFrame({
|
||||
<ViewFixtureProvider fixtures={fixtures}>
|
||||
<Suspense fallback={<Spinner size='large' centered />}>
|
||||
<FixtureBoundary key={`${definition.name}:${fixture}:${content}`}>
|
||||
{definition.context === 'server' ? (
|
||||
<ServerContext.Provider>{view}</ServerContext.Provider>
|
||||
) : (
|
||||
view
|
||||
)}
|
||||
<EngineSlots.Provider value={engineSlots}>
|
||||
{definition.context === 'server' ? (
|
||||
<ServerContext.Provider>{view}</ServerContext.Provider>
|
||||
) : (
|
||||
view
|
||||
)}
|
||||
</EngineSlots.Provider>
|
||||
</FixtureBoundary>
|
||||
</Suspense>
|
||||
</ViewFixtureProvider>
|
||||
|
||||
@@ -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 }) => (
|
||||
|
||||
@@ -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(
|
||||
<PermissionRoute permission={permission}>
|
||||
<ViewSessionProvider>
|
||||
{slots && <Slot name={slots[0]} data={data} />}
|
||||
<Spinner.Suspense>{Frame ? <Frame>{page}</Frame> : page}</Spinner.Suspense>
|
||||
<EngineSlots.Provider value={slots ?? noSlots}>
|
||||
<Spinner.Suspense>{Frame ? <Frame>{page}</Frame> : page}</Spinner.Suspense>
|
||||
</EngineSlots.Provider>
|
||||
{slots && <Slot name={slots[1]} data={data} />}
|
||||
</ViewSessionProvider>
|
||||
</PermissionRoute>
|
||||
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
type ViewProps,
|
||||
} from './registry';
|
||||
import { useViewDecision } from './session';
|
||||
import { SlotCheck } from './slotCheck';
|
||||
|
||||
const ViewAreaContext = createContext<readonly ViewContext[]>([]);
|
||||
|
||||
@@ -293,12 +294,14 @@ function ViewBody({ definition, model, inputs, className, children }: BodyProps
|
||||
}
|
||||
|
||||
const chain = (
|
||||
<ViewChain
|
||||
name={definition.name}
|
||||
layers={layers}
|
||||
placeholder={placeholder}
|
||||
frame={{ model, inputs, className, children }}
|
||||
/>
|
||||
<SlotCheck definition={definition} layer={layers.at(-1)}>
|
||||
<ViewChain
|
||||
name={definition.name}
|
||||
layers={layers}
|
||||
placeholder={placeholder}
|
||||
frame={{ model, inputs, className, children }}
|
||||
/>
|
||||
</SlotCheck>
|
||||
);
|
||||
|
||||
return inspection ? (
|
||||
|
||||
@@ -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': () => (
|
||||
<>
|
||||
<Slot name={before} />
|
||||
<p>classic</p>
|
||||
<Slot name={after} />
|
||||
</>
|
||||
),
|
||||
});
|
||||
});
|
||||
|
||||
let warn: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
clearThemeLayers();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
async function renderWith(layer: ComponentType<Props>, engine: readonly string[] = []) {
|
||||
registerViewLayer('aurora', 'theme', { 'test.slotted': layer });
|
||||
render(
|
||||
<EngineSlots.Provider value={engine}>
|
||||
<View name='test.slotted' />
|
||||
</EngineSlots.Provider>
|
||||
);
|
||||
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(() => <p>aurora</p>);
|
||||
|
||||
expect(warn).toHaveBeenCalledWith(
|
||||
`[views] The "aurora" layer of "test.slotted" renders none of its slots (${before}, ${after}). ` +
|
||||
'Render them with <Slot name="…"/>, or render <Default/>, so extensions keep their place.'
|
||||
);
|
||||
});
|
||||
|
||||
it('stays quiet for a layer that renders a slot or the layer below', async () => {
|
||||
await renderWith(() => <Slot name={after} />);
|
||||
cleanup();
|
||||
await renderWith(({ Default }) => (
|
||||
<section>
|
||||
<Default />
|
||||
</section>
|
||||
));
|
||||
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('leaves out the slots the engine renders around a page', async () => {
|
||||
await renderWith(() => <p>aurora</p>, [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': () => <Slot name={before} /> });
|
||||
await renderWith(() => <View name='test.nested' />);
|
||||
|
||||
expect(warn).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -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<readonly string[]>([]);
|
||||
|
||||
/** 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 <Slot name="…"/>, or render <Default/>, so extensions keep their place.'
|
||||
);
|
||||
}
|
||||
}, SLOT_CHECK_DELAY_MS);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [owner, definition.name, expected]);
|
||||
|
||||
return <SlotRecorder.Provider value={owner === undefined ? null : record}>{children}</SlotRecorder.Provider>;
|
||||
}
|
||||
Reference in New Issue
Block a user