Skip to content

Commit

Permalink
feat!: add htmlAttributes and bodyContent.attributes render params (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS committed Mar 20, 2024
1 parent d2465b0 commit f99b4ff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 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
8 changes: 3 additions & 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 All @@ -88,6 +84,7 @@ export interface RenderParams<Data, Plugins extends Plugin[] = []> extends Commo
icon?: Icon;
nonce?: string;
// content
htmlAttributes?: Attributes;
meta?: Meta[];
links?: Link[];
scripts?: Script[];
Expand All @@ -97,6 +94,7 @@ export interface RenderParams<Data, Plugins extends Plugin[] = []> extends Commo
bodyContent?: {
theme?: string;
className?: string;
attributes?: Attributes;
beforeRoot?: string;
root?: string;
afterRoot?: string;
Expand Down
31 changes: 20 additions & 11 deletions src/utils/generateRenderContent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import htmlescape from 'htmlescape';

import type {BodyContent, Icon, Meta, Plugin, RenderContent, RenderParams} from '../types.js';
import type {
Attributes,
BodyContent,
Icon,
Meta,
Plugin,
RenderContent,
RenderParams,
} from '../types.js';
import {getRenderHelpers, hasProperty} from '../utils.js';

function getRootClassName(theme?: string) {
Expand Down Expand Up @@ -38,7 +46,7 @@ export function generateRenderContent<Plugins extends Plugin[], Data>(
params: RenderParams<Data, Plugins>,
): RenderContent {
const helpers = getRenderHelpers(params);
const htmlAttributes: Record<string, string> = {};
const htmlAttributes: Attributes = {...params.htmlAttributes};
const meta = params.meta ?? [];
// in terms of sets: meta = params.meta ∪ (defaultMeta ∖ params.meta)
defaultMeta.forEach((defaultMetaItem) => {
Expand All @@ -55,13 +63,19 @@ 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(
const bodyClassName = Array.from(
new Set([...getRootClassName(theme), ...(className ? className.split(' ') : [])]),
);
)
.filter(Boolean)
.join(' ');

const bodyContent: BodyContent = {
attributes: {},
className: bodyClasses,
attributes: {
class: bodyClassName,
...content.attributes,
},
root: content.root,
beforeRoot: content.beforeRoot ? [content.beforeRoot] : [],
afterRoot: content.afterRoot ? [content.afterRoot] : [],
Expand Down Expand Up @@ -99,11 +113,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 f99b4ff

Please sign in to comment.