Skip to content

Commit

Permalink
feat(pugins)!: allow to set body attributes
Browse files Browse the repository at this point in the history
removed className from bodyContent
  • Loading branch information
ValeraS committed Mar 20, 2024
1 parent d0719d2 commit 5c91730
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ export interface HeadContent {

export interface BodyContent {
attributes: Attributes;
/** @deprecated use attributes.class instead */
className: string[];
beforeRoot: string[];
root?: string;
afterRoot: string[];
Expand Down Expand Up @@ -335,7 +333,7 @@ Adds Yandex metrics counters on the page.
Usage:

```js
import {createMiddleware, createYandexMetrikaPlugin} from '@gravity-ui/app-layout';
import {createRenderFunction, createYandexMetrikaPlugin} from '@gravity-ui/app-layout';

const renderLayout = createRenderFunction([createYandexMetrikaPlugin()]);

Expand Down Expand Up @@ -395,7 +393,7 @@ Adds script and styles from webpack assets manifest file.
Usage:

```js
import {createMiddleware, createLayoutPlugin} from '@gravity-ui/app-layout';
import {createRenderFunction, createLayoutPlugin} from '@gravity-ui/app-layout';

const renderLayout = createRenderFunction([
createLayoutPlugin({manifest: 'path/to/assets-manifest.json', publicPath: '/build/'}),
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/google-analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export function createGoogleAnalyticsPlugin(): Plugin<
> {
return {
name: 'googleAnalytics',
apply({options, renderContent, utils}) {
apply({options, renderContent}) {
if (!options || !options.counter) {
return;
}

renderContent.bodyContent.afterRoot.push(renderGoogleAnalyticsCounter(options, utils));
renderContent.bodyContent.afterRoot.push(
renderGoogleAnalyticsCounter(options, renderContent.helpers),
);
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/yandex-metrika/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export type {MetrikaCounter, MetrikaPluginOptions, UserParams} from './types.js'
export function createYandexMetrikaPlugin(): Plugin<MetrikaPluginOptions, 'yandexMetrika'> {
return {
name: 'yandexMetrika',
apply({options, renderContent, utils}) {
apply({options, renderContent}) {
if (!options || !options.counter) {
return;
}

renderContent.bodyContent.afterRoot.push(renderMetrika(options, utils));
renderContent.bodyContent.afterRoot.push(renderMetrika(options, renderContent.helpers));
},
};
}
Expand Down
6 changes: 1 addition & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ export interface HeadContent {

export interface BodyContent {
attributes: Attributes;
/** @deprecated use attributes.class instead */
className: string[];
beforeRoot: string[];
root?: string;
afterRoot: string[];
}

export type OldBodyContent = Omit<BodyContent, 'attributes'>;

export interface RenderContent extends HeadContent {
htmlAttributes: Attributes;
bodyContent: BodyContent;
Expand All @@ -77,7 +73,7 @@ export interface Plugin<Options = any, Name extends string = string> {
name: Name;
apply: (params: {
options: Options | undefined;
renderContent: Omit<RenderContent, 'bodyContent'> & {bodyContent: OldBodyContent};
renderContent: RenderContent;
commonOptions: CommonOptions;
/** @deprecated use `renderContent.helpers` instead */
utils: RenderHelpers;
Expand Down
21 changes: 11 additions & 10 deletions src/utils/generateRenderContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,24 @@ 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: BodyContent = {
attributes: {},
className: bodyClasses,
root: content.root,
beforeRoot: content.beforeRoot ? [content.beforeRoot] : [],
afterRoot: content.afterRoot ? [content.afterRoot] : [],
};

const {theme, className} = content;
if (theme || className) {
const bodyClasses = Array.from(
new Set([...getRootClassName(theme), ...(className ? className.split(' ') : [])]),
)
.filter(Boolean)
.join(' ');
bodyContent.attributes.class = bodyClasses;
}

const icon: Icon = {
...defaultIcon,
...params.icon,
Expand Down Expand Up @@ -99,11 +105,6 @@ export function generateRenderContent<Plugins extends Plugin[], Data>(
});
}

bodyContent.attributes = {
...bodyContent.attributes,
class: bodyContent.className.filter(Boolean).join(' '),
};

if (lang) {
htmlAttributes.lang = lang;
}
Expand Down

0 comments on commit 5c91730

Please sign in to comment.