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!: add htmlAttributes and bodyContent.attributes render params #41

Merged
merged 1 commit into from
Mar 20, 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
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 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 @@ -73,11 +69,11 @@
renderLink(link: Link): string;
attrs(obj: Attributes): string;
}
export interface Plugin<Options = any, Name extends string = string> {

Check warning on line 72 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;
renderContent: Omit<RenderContent, 'bodyContent'> & {bodyContent: OldBodyContent};
renderContent: RenderContent;
commonOptions: CommonOptions;
/** @deprecated use `renderContent.helpers` instead */
utils: RenderHelpers;
Expand All @@ -88,6 +84,7 @@
icon?: Icon;
nonce?: string;
// content
htmlAttributes?: Attributes;
meta?: Meta[];
links?: Link[];
scripts?: Script[];
Expand All @@ -97,6 +94,7 @@
bodyContent?: {
theme?: string;
className?: string;
attributes?: Attributes;
beforeRoot?: string;
root?: string;
afterRoot?: string;
Expand All @@ -112,7 +110,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 113 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

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

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
Loading