Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calculate initial body classes in @gravity-ui/uikit notation if… #28

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ interface RenderParams<Data, Plugins> {

// content of body tag
bodyContent?: {
// initial application theme if @gravity-ui/uikit is used
theme?: string;
// class name for body tag
className?: string;
// body content before div tag with id root
Expand Down Expand Up @@ -252,6 +254,7 @@ interface RenderContent {
inlineScripts: string[];
inlineStyleSheets: string[];
bodyContent: {
theme?: string;
className: string[];
beforeRoot: string[];
root?: string;
Expand Down
25 changes: 25 additions & 0 deletions src/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,28 @@ test('should render root content', () => {
/<div id="root">\s*content\s*<\/div>/,
);
});

test('should render body classes', () => {
expect(
createRenderFunction()({
title: 'Foobar',
bodyContent: {root: 'content', className: 'test'},
}),
).toMatch(/<body class="test">\s*<div id="root">\s*content\s*<\/div>\s*<\/body>/);
expect(
createRenderFunction()({
title: 'Foobar',
bodyContent: {root: 'content', theme: 'light'},
}),
).toMatch(
/<body class="g-root g-root_theme_light">\s*<div id="root">\s*content\s*<\/div>\s*<\/body>/,
);
expect(
createRenderFunction()({
title: 'Foobar',
bodyContent: {root: 'content', className: 'test', theme: 'light'},
}),
).toMatch(
/<body class="g-root g-root_theme_light test">\s*<div id="root">\s*content\s*<\/div>\s*<\/body>/,
);
});
17 changes: 16 additions & 1 deletion src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import htmlescape from 'htmlescape';
import {attrs, getRenderHelpers, hasProperty} from './utils.js';
import type {Icon, Meta, RenderParams, Plugin, RenderContent} from './types.js';

function getRootClassName(theme?: string) {
if (!theme) {
return [];
}
const classes = ['g-root'];
if (theme) {
classes.push(`g-root_theme_${theme}`);
}
return classes;
}

const defaultIcon: Icon = {
type: 'image/png',
sizes: '16x16',
Expand Down Expand Up @@ -37,8 +48,12 @@ export function generateRenderContent<Plugins extends Plugin[], Data>(
inlineScripts.unshift(`window.__DATA__ = ${htmlescape(params.data || {})};`);

const content = params.bodyContent ?? {};
const {theme, className} = content;
const bodyClasses = Array.from(
new Set([...getRootClassName(theme), ...(className ? className.split(' ') : [])]),
);
const bodyContent = {
className: content.className ? [content.className] : [],
className: bodyClasses,
root: content.root,
beforeRoot: content.beforeRoot ? [content.beforeRoot] : [],
afterRoot: content.afterRoot ? [content.afterRoot] : [],
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
renderMeta(meta: Meta): string;
renderLink(link: Link): string;
}
export interface Plugin<Options = any, Name extends string = string> {

Check warning on line 62 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
name: Name;
apply: (params: {
options: Options | undefined;
Expand All @@ -80,6 +80,7 @@
inlineScripts?: string[];
inlineStyleSheets?: string[];
bodyContent?: {
theme?: string;
className?: string;
beforeRoot?: string;
root?: string;
Expand All @@ -96,7 +97,7 @@
? {[K in Name & string]: Options}
: {};

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void

Check warning on line 100 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
? I
: never;

Expand Down
Loading