UNPKG

@dash-ui/react

Version:

React components and hooks for dash-ui

1 lines 4.85 kB
{"version":3,"file":"index.dev.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","key","nonce","sheet","join","__html","Style","createGatsbyRenderer","replaceRenderer","props","bodyString","require","renderToStaticMarkup","bodyComponent","setHeadComponents"],"mappings":";;;;;AAAA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASA,WAAT,CACLC,IADK,EAELC,QAFK,EAGLC,OAHK,EAMQ;EAAA;;EAAA,IAJbD,QAIa;IAJbA,QAIa,GAJcE,MAId;;;EAAA,IAHbD,OAGa;IAHbA,OAGa,GADT,EACS;;;EACb,IAAM;IAAEE;MAASH,QAAjB;EACA,IAAM;IAAEI,KAAF;IAASC;MAAQC,sBAAsB,CAACP,IAAD,EAAOC,QAAP,CAA7C;EAEA,oBACE;IACE,GAAG,EAAEG,IAAI,CAACI,GADZ;IAEE,KAAK,oBAAEN,OAAO,CAACO,KAAV,2DAAoBL,IAAI,CAACM,KAAL,CAAWD,KAAX,GAAmBL,IAAI,CAACM,KAAL,CAAWD,KAA9B,GAAsC,KAAK,CAFtE;IAGE,aAAWJ,KAAK,CAACM,IAAN,CAAW,GAAX,CAHb;IAIE,cAAYP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