@dash-ui/react
Version:
React components and hooks for dash-ui
1 lines • 4.23 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../src/index.tsx"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { styles as defaultStyles } from \"@dash-ui/styles\";\nimport type { Styles } from \"@dash-ui/styles\";\nimport { createStylesFromString } from \"@dash-ui/styles/server\";\nimport * as React from \"react\";\n\n/**\n * A function for creating a React `<style>` component for\n * inserting Dash styles in SSR.\n *\n * @param html - The HTML generated by `renderToStaticMarkup()` or `renderToString()`\n * @param styles - An instance of `styles()`. Defaults to the default styles instance\n * in `@dash-ui/styles`.\n * @param options\n * @param options.nonce\n */\nexport function toComponent(\n html: string,\n styles: Styles<any, any> = defaultStyles,\n options: {\n nonce?: string;\n } = {}\n): JSX.Element {\n const { dash } = styles;\n const { names, css } = createStylesFromString(html, styles);\n\n return (\n <style\n key={dash.key}\n nonce={options.nonce ?? (dash.sheet.nonce ? dash.sheet.nonce : void 0)}\n data-dash={names.join(\" \")}\n data-cache={dash.key}\n dangerouslySetInnerHTML={{ __html: css }}\n />\n );\n}\n\n/**\n * A React component for injecting SSR CSS styles into Next.js documents\n *\n * @param root0\n * @param root0.html\n * @param root0.styles\n * @param root0.nonce\n * @example\n * // _document.js\n * import React from 'react'\n * import Document from 'next/document'\n * import {Style} from '@dash-ui/react/server'\n *\n * export default class MyDocument extends Document {\n * static async getInitialProps(ctx) {\n * const initialProps = await Document.getInitialProps(ctx)\n * return {\n * ...initialProps,\n * styles: (\n * <>\n * {initialProps.styles}\n * <Style html={initialProps.html}/>\n * </>\n * ),\n * }\n * }\n * }\n */\nexport function Style({ html, styles, nonce }: StyleProps): JSX.Element {\n return toComponent(html, styles, { nonce });\n}\n\nexport interface StyleProps {\n /**\n * The HTML generated by Next.js, `renderToStaticMarkup()` or `renderToString()`\n */\n html: string;\n /**\n * An instance of `styles()`. Defaults to the default styles instance in `@dash-ui/styles`.\n */\n styles?: Styles<any, any>;\n /**\n * A nonce for the `<style>` tag.\n */\n nonce?: Styles<any, any>[\"dash\"][\"sheet\"][\"nonce\"];\n}\n\n/**\n * Creates a Gatsby replaceRenderer for injecting styles generated by Dash on\n * the server into the Gatsby `<head>` component\n *\n * @param styles - An instance of `styles()`. Defaults to the default styles instance\n * in `@dash-ui/styles`.\n * @example\n * // gatsby-ssr.js\n * exports.replaceRenderer = require('@dash-ui/react/server').createGatsbyRenderer()\n */\nexport function createGatsbyRenderer(styles: Styles<any, any> = defaultStyles) {\n /* istanbul ignore next */\n return function replaceRenderer<P = any>(props: P): P {\n const bodyString = require(\"react-dom/server\").renderToStaticMarkup(\n // @ts-expect-error\n props.bodyComponent\n );\n // @ts-expect-error\n props.setHeadComponents([toComponent(bodyString, styles)]);\n return props;\n };\n}\n\nexport * from \"@dash-ui/styles/server\";\n"],"names":["toComponent","html","styles","options","defaultStyles","dash","names","css","createStylesFromString","__reactCreateElement__","key","nonce","sheet","data-dash","join","data-cache","dangerouslySetInnerHTML","__html","Style","createGatsbyRenderer","props","bodyString","require","renderToStaticMarkup","bodyComponent","setHeadComponents"],"mappings":"AAgBO,SAASA,EACdC,EACAC,EACAC,GAGa,eAJbD,IAAAA,EAA2BE,YAC3BD,IAAAA,EAEI,IAEJ,IAAME,KAAEA,GAASH,GACXI,MAAEA,EAAFC,IAASA,GAAQC,EAAuBP,EAAMC,GAEpD,OACEO,WACEC,IAAKL,EAAKK,IACVC,gBAAOR,EAAQQ,qBAAUN,EAAKO,MAAMD,MAAQN,EAAKO,MAAMD,WAAQ,EAC/DE,YAAWP,EAAMQ,KAAK,KACtBC,aAAYV,EAAKK,IACjBM,wBAAyB,CAAEC,OAAQV,KAiClC,SAASW,KAAwD,IAAlDjB,KAAEA,EAAFC,OAAQA,EAARS,MAAgBA,KACpC,OAAOX,EAAYC,EAAMC,EAAQ,CAAES,MAAAA,IA4B9B,SAASQ,EAAqBjB,GAEnC,gBAFmCA,IAAAA,EAA2BE,GAEvD,SAAkCgB,GACvC,IAAMC,EAAaC,QAAQ,oBAAoBC,qBAE7CH,EAAMI,eAIR,OADAJ,EAAMK,kBAAkB,CAACzB,EAAYqB,EAAYnB,KAC1CkB"}