Skip to content

Commit

Permalink
feat: calculate initial body classes in @gravity-ui/uikit notation if… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Raubzeug authored Jan 12, 2024
1 parent 7facc53 commit 4e3f89f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
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 @@ -80,6 +80,7 @@ export interface RenderParams<Data, Plugins extends Plugin[] = []> extends Commo
inlineScripts?: string[];
inlineStyleSheets?: string[];
bodyContent?: {
theme?: string;
className?: string;
beforeRoot?: string;
root?: string;
Expand Down

0 comments on commit 4e3f89f

Please sign in to comment.