Update renderToCanvas functions to use precise type

This commit is contained in:
Tom Coleman 2022-11-04 14:08:05 +11:00
parent 2becfed339
commit 91fa988940
10 changed files with 18 additions and 19 deletions

View File

@ -20,7 +20,7 @@ let lastPromise = app.boot();
let hasRendered = false; let hasRendered = false;
let isRendering = false; let isRendering = false;
function render(options: OptionsArgs, el: Element) { function render(options: OptionsArgs, el: EmberFramework['canvasElement']) {
if (isRendering) return; if (isRendering) return;
isRendering = true; isRendering = true;
@ -62,7 +62,7 @@ function render(options: OptionsArgs, el: Element) {
export function renderToCanvas( export function renderToCanvas(
{ storyFn, kind, name, showMain, showError }: Store_RenderContext<EmberFramework>, { storyFn, kind, name, showMain, showError }: Store_RenderContext<EmberFramework>,
domElement: Element domElement: EmberFramework['canvasElement']
) { ) {
const element = storyFn(); const element = storyFn();

View File

@ -28,7 +28,7 @@ import type {
} from './csf'; } from './csf';
export interface WebFramework extends Framework { export interface WebFramework extends Framework {
rootElement: HTMLElement; canvasElement: HTMLElement;
} }
export type Store_ModuleExport = any; export type Store_ModuleExport = any;

View File

@ -1,5 +1,4 @@
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
// @ts-expect-error (Converted from ts-ignore)
import global from 'global'; import global from 'global';
import { dedent } from 'ts-dedent'; import { dedent } from 'ts-dedent';
@ -43,7 +42,7 @@ export const render: ArgsStoryFn<HtmlFramework> = (args, context) => {
export function renderToCanvas( export function renderToCanvas(
{ storyFn, kind, name, showMain, showError, forceRemount }: Store_RenderContext<HtmlFramework>, { storyFn, kind, name, showMain, showError, forceRemount }: Store_RenderContext<HtmlFramework>,
domElement: Element domElement: HtmlFramework['canvasElement']
) { ) {
const element = storyFn(); const element = storyFn();
showMain(); showMain();

View File

@ -37,7 +37,7 @@ const StoryHarness: preact.FunctionalComponent<{
title: string; title: string;
showError: Store_RenderContext<PreactFramework>['showError']; showError: Store_RenderContext<PreactFramework>['showError'];
storyFn: () => any; storyFn: () => any;
domElement: Element; domElement: PreactFramework['canvasElement'];
}> = ({ showError, name, title, storyFn, domElement }) => { }> = ({ showError, name, title, storyFn, domElement }) => {
const content = preact.h(storyFn as any, null); const content = preact.h(storyFn as any, null);
if (!content) { if (!content) {
@ -55,7 +55,7 @@ const StoryHarness: preact.FunctionalComponent<{
export function renderToCanvas( export function renderToCanvas(
{ storyFn, title, name, showMain, showError, forceRemount }: Store_RenderContext<PreactFramework>, { storyFn, title, name, showMain, showError, forceRemount }: Store_RenderContext<PreactFramework>,
domElement: Element domElement: PreactFramework['canvasElement']
) { ) {
if (forceRemount) { if (forceRemount) {
preactRender(null, domElement); preactRender(null, domElement);

View File

@ -136,7 +136,7 @@ export async function renderToCanvas(
showException, showException,
forceRemount, forceRemount,
}: Store_RenderContext<ReactFramework>, }: Store_RenderContext<ReactFramework>,
domElement: Element domElement: ReactFramework['canvasElement']
) { ) {
const Story = unboundStoryFn as FC<StoryContext<ReactFramework>>; const Story = unboundStoryFn as FC<StoryContext<ReactFramework>>;

View File

@ -56,7 +56,7 @@ export async function renderToCanvas(
storyContext, storyContext,
storyContext: { parameters, args, argTypes }, storyContext: { parameters, args, argTypes },
}: Store_RenderContext<ServerFramework>, }: Store_RenderContext<ServerFramework>,
domElement: Element domElement: ServerFramework['canvasElement']
) { ) {
// Some addons wrap the storyFn so we need to call it even though Server doesn't need the answer // Some addons wrap the storyFn so we need to call it even though Server doesn't need the answer
storyFn(); storyFn();

View File

@ -5,9 +5,9 @@ import PreviewRender from '@storybook/svelte/templates/PreviewRender.svelte';
import type { SvelteFramework } from './types'; import type { SvelteFramework } from './types';
const componentsByDomElement = new Map<Element, SvelteComponentTyped>(); const componentsByDomElement = new Map<SvelteFramework['canvasElement'], SvelteComponentTyped>();
function teardown(domElement: Element) { function teardown(domElement: SvelteFramework['canvasElement']) {
if (!componentsByDomElement.has(domElement)) { if (!componentsByDomElement.has(domElement)) {
return; return;
} }
@ -29,7 +29,7 @@ export function renderToCanvas(
storyContext, storyContext,
forceRemount, forceRemount,
}: Store_RenderContext<SvelteFramework>, }: Store_RenderContext<SvelteFramework>,
domElement: Element domElement: SvelteFramework['canvasElement']
) { ) {
const existingComponent = componentsByDomElement.get(domElement); const existingComponent = componentsByDomElement.get(domElement);

View File

@ -8,7 +8,7 @@ import type { VueFramework } from './types';
export const COMPONENT = 'STORYBOOK_COMPONENT'; export const COMPONENT = 'STORYBOOK_COMPONENT';
export const VALUES = 'STORYBOOK_VALUES'; export const VALUES = 'STORYBOOK_VALUES';
const map = new Map<Element, Instance>(); const map = new Map<VueFramework['canvasElement'], Instance>();
type Instance = CombinedVueInstance< type Instance = CombinedVueInstance<
Vue, Vue,
{ {
@ -20,7 +20,7 @@ type Instance = CombinedVueInstance<
Record<never, any> Record<never, any>
>; >;
const getRoot = (domElement: Element): Instance => { const getRoot = (domElement: VueFramework['canvasElement']): Instance => {
const cachedInstance = map.get(domElement); const cachedInstance = map.get(domElement);
if (cachedInstance != null) return cachedInstance; if (cachedInstance != null) return cachedInstance;
@ -92,13 +92,13 @@ export function renderToCanvas(
showException, showException,
forceRemount, forceRemount,
}: Store_RenderContext<VueFramework>, }: Store_RenderContext<VueFramework>,
domElement: Element domElement: VueFramework['canvasElement']
) { ) {
const root = getRoot(domElement); const root = getRoot(domElement);
Vue.config.errorHandler = showException; Vue.config.errorHandler = showException;
const element = storyFn(); const element = storyFn();
let mountTarget: Element | null; let mountTarget: VueFramework['canvasElement'] | null;
// Vue2 mount always replaces the mount target with Vue-generated DOM. // Vue2 mount always replaces the mount target with Vue-generated DOM.
// https://v2.vuejs.org/v2/api/#el:~:text=replaced%20with%20Vue%2Dgenerated%20DOM // https://v2.vuejs.org/v2/api/#el:~:text=replaced%20with%20Vue%2Dgenerated%20DOM

View File

@ -20,11 +20,11 @@ export const setup = (fn: (app: any) => void) => {
setupFunction = fn; setupFunction = fn;
}; };
const map = new Map<Element, ReturnType<typeof createApp>>(); const map = new Map<VueFramework['canvasElement'], ReturnType<typeof createApp>>();
export function renderToCanvas( export function renderToCanvas(
{ title, name, storyFn, showMain, showError, showException }: Store_RenderContext<VueFramework>, { title, name, storyFn, showMain, showError, showException }: Store_RenderContext<VueFramework>,
domElement: Element domElement: VueFramework['canvasElement']
) { ) {
// TODO: explain cyclical nature of these app => story => mount // TODO: explain cyclical nature of these app => story => mount
let element: StoryFnVueReturnType; let element: StoryFnVueReturnType;

View File

@ -38,7 +38,7 @@ export function renderToCanvas(
showError, showError,
forceRemount, forceRemount,
}: Store_RenderContext<WebComponentsFramework>, }: Store_RenderContext<WebComponentsFramework>,
domElement: Element domElement: WebComponentsFramework['canvasElement']
) { ) {
const element = storyFn(); const element = storyFn();