diff --git a/website/astro.config.js b/website/astro.config.js index a3e6ff2c8..1a5ff23ab 100644 --- a/website/astro.config.js +++ b/website/astro.config.js @@ -2,7 +2,10 @@ import { defineConfig } from 'astro/config' import { unified } from '@astrojs/markdown-remark' import starlight from '@astrojs/starlight' import llmsTxt from 'starlight-llms-txt' -import { rehypeTrailingSlash } from './rehype.js' +import { + rehypeExternalLinks, + rehypeTrailingSlash +} from './rehype.js' const isProduction = process.env.NODE_ENV === 'production' const chartTags = [ @@ -23,9 +26,30 @@ export default defineConfig({ trailingSlash: 'always', markdown: { processor: unified({ - rehypePlugins: [rehypeTrailingSlash] + // Docusaurus kept straight quotes and dots, and error messages are quoted as they are + smartypants: false, + rehypePlugins: [rehypeTrailingSlash, rehypeExternalLinks] }) }, + vite: { + plugins: [ + { + name: 'sandbox-index', + // Unlike GitHub Pages, the dev server does not answer a directory of `public` with its index.html + configureServer(server) { + server.middlewares.use((req, _res, next) => { + const [path, query] = (req.url ?? '').split('?') + + if (path.startsWith('/sandboxes/') && path.endsWith('/')) { + req.url = `${path}index.html${query === undefined ? '' : `?${query}`}` + } + + next() + }) + } + } + ] + }, // Pages of the Docusaurus website that have no counterpart redirects: { '/search': '/', @@ -68,13 +92,19 @@ export default defineConfig({ label: 'GitHub', icon: 'github', href: 'https://github.com/reactchartjs/react-chartjs-2' + }, + { + label: 'Questions in GitHub Discussions', + icon: 'comment-alt', + href: 'https://github.com/reactchartjs/react-chartjs-2/discussions/categories/q-a' } ], editLink: { baseUrl: 'https://github.com/reactchartjs/react-chartjs-2/edit/master/website/' }, components: { - Sidebar: './src/components/Sidebar.astro' + PageSidebar: './src/components/PageSidebar.astro', + Footer: './src/components/Footer.astro' }, plugins: [ llmsTxt({ diff --git a/website/rehype.js b/website/rehype.js index 314bc2469..9fc6fabc8 100644 --- a/website/rehype.js +++ b/website/rehype.js @@ -1,3 +1,22 @@ +const SITE = 'https://react-chartjs-2.js.org' + +/** + * Calls the callback for every link of the tree, including the links inside JSX of MDX files. + * @param {import('hast').Parent} node - The node to walk. + * @param {(link: import('hast').Element) => void} callback - What to do with a link. + */ +function visitLinks(node, callback) { + for (const child of node.children ?? []) { + if (child.type === 'element' && child.tagName === 'a') { + callback(child) + } + + if ('children' in child) { + visitLinks(child, callback) + } + } +} + /** * Appends the trailing slash to internal links of pages, so they lead straight to the page * instead of a redirect: `/docs/migration-to-v4#tree-shaking` becomes `/docs/migration-to-v4/#tree-shaking`. @@ -19,25 +38,27 @@ function addTrailingSlash(href) { return `${pathname}/${rest}` } -/** - * @param {import('hast').Parent} node - The node to walk. - */ -function visitLinks(node) { - for (const child of node.children ?? []) { - if (child.type === 'element') { - if (child.tagName === 'a') { - const href = addTrailingSlash(child.properties?.href) - - if (href) { - child.properties.href = href - } - } +export function rehypeTrailingSlash() { + return tree => visitLinks(tree, (link) => { + const href = addTrailingSlash(link.properties?.href) - visitLinks(child) + if (href) { + link.properties.href = href } - } + }) } -export function rehypeTrailingSlash() { - return visitLinks +/** + * Opens links to other sites in a new tab, as the Docusaurus website did. + * @returns {(tree: import('hast').Root) => void} The transformer of the page. + */ +export function rehypeExternalLinks() { + return tree => visitLinks(tree, (link) => { + const { href } = link.properties ?? {} + + if (typeof href === 'string' && /^https?:\/\//.test(href) && !href.startsWith(SITE)) { + link.properties.target = '_blank' + link.properties.rel = ['noopener', 'noreferrer'] + } + }) } diff --git a/website/src/components/Bwndw.astro b/website/src/components/Bwndw.astro new file mode 100644 index 000000000..de9621f11 --- /dev/null +++ b/website/src/components/Bwndw.astro @@ -0,0 +1,47 @@ +--- +interface Props { + /** The id of the block, unique on the page. */ + id: string + /** Whether the block is for wide screens, which show the right sidebar, or for the others. */ + wide: boolean + class?: string +} + +const { id, wide, class: className } = Astro.props +--- + +
+
+
+ + + diff --git a/website/src/components/Footer.astro b/website/src/components/Footer.astro new file mode 100644 index 000000000..600c3657b --- /dev/null +++ b/website/src/components/Footer.astro @@ -0,0 +1,16 @@ +--- +import Default from '@astrojs/starlight/components/Footer.astro' +import Bwndw from './Bwndw.astro' + +// The pages with the right sidebar, where the other block is +const { toc } = Astro.locals.starlightRoute +--- + +{toc && ( + <> + + {/* After both blocks, so the one that does not fit the screen is gone when it runs */} + + +)} + diff --git a/website/src/components/PageSidebar.astro b/website/src/components/PageSidebar.astro new file mode 100644 index 000000000..6d24dbe68 --- /dev/null +++ b/website/src/components/PageSidebar.astro @@ -0,0 +1,11 @@ +--- +import Default from '@astrojs/starlight/components/PageSidebar.astro' +import Bwndw from './Bwndw.astro' + +const [title, ...headings] = Astro.locals.starlightRoute.toc?.items ?? [] +// A table of contents with the page title alone has nothing to navigate +const hasContents = !(title?.children.length === 0 && headings.length === 0) +--- + +{hasContents ? : } + diff --git a/website/src/components/PropsTable.astro b/website/src/components/PropsTable.astro index 1c61db203..614553bd9 100644 --- a/website/src/components/PropsTable.astro +++ b/website/src/components/PropsTable.astro @@ -46,6 +46,7 @@ if (!doc) { function formatType(type: string) { return type .replace(/ \| undefined$/, '') + .replace(/ChartJSOrUndefined<([^>]+)>/, 'Chart<$1>') .replace(/^_DeepPartialObject]+)>[\s\S]*$/, 'ChartOptions<$1>') } @@ -78,7 +79,7 @@ const props = Object.values(doc.props).map(({ {prop.see && ( <> {' '} - See more + See more )}

diff --git a/website/src/components/Sidebar.astro b/website/src/components/Sidebar.astro deleted file mode 100644 index 7eac416e3..000000000 --- a/website/src/components/Sidebar.astro +++ /dev/null @@ -1,27 +0,0 @@ ---- -import Default from '@astrojs/starlight/components/Sidebar.astro' ---- - - -
-
-
- - - diff --git a/website/src/content/docs/components/bubble.mdx b/website/src/content/docs/components/bubble.mdx index 7f57c0b85..e4685764c 100644 --- a/website/src/content/docs/components/bubble.mdx +++ b/website/src/content/docs/components/bubble.mdx @@ -1,6 +1,7 @@ --- title: "Bubble" description: "Description of Bubble component from react-chartjs-2." +next: false --- import PropsTable from '../../../components/PropsTable.astro' diff --git a/website/src/content/docs/components/chart.mdx b/website/src/content/docs/components/chart.mdx index ca17b8245..aaef14878 100644 --- a/website/src/content/docs/components/chart.mdx +++ b/website/src/content/docs/components/chart.mdx @@ -1,6 +1,7 @@ --- title: "Chart" description: "Description of Chart component from react-chartjs-2." +prev: Components --- import PropsTable from '../../../components/PropsTable.astro' diff --git a/website/src/content/docs/components/index.md b/website/src/content/docs/components/index.md index 21bc9b32b..9d57111a9 100644 --- a/website/src/content/docs/components/index.md +++ b/website/src/content/docs/components/index.md @@ -1,6 +1,7 @@ --- title: "Components" description: "List of react-chartjs-2 components." +prev: false --- Every component renders a `` and creates a Chart.js chart on it. Typed components set the chart type for you; `Chart` takes it as the `type` prop. diff --git a/website/src/content/docs/docs/chartjs-v2.mdx b/website/src/content/docs/docs/chartjs-v2.mdx index 9e3278c30..e0ec5bc69 100644 --- a/website/src/content/docs/docs/chartjs-v2.mdx +++ b/website/src/content/docs/docs/chartjs-v2.mdx @@ -1,6 +1,7 @@ --- title: "Using with Chart.js v2" description: "Using react-chartjs-2 with Chart.js v2" +next: false --- import { Tabs, TabItem } from '@astrojs/starlight/components' @@ -35,7 +36,7 @@ Also, please consider upgrading your app to [Chart.js v3](#upgrading-to-chartjs- ## Upgrading to Chart.js v3 -First, upgrade packages. You'll need to install Chart.js v3 and the latest version of this library: +First, upgrade packages. You'll need to install Chart.js v3 and a compatible version of this library: @@ -61,4 +62,4 @@ First, upgrade packages. You'll need to install Chart.js v3 and the latest versi -Then, please follow the [this guide](/docs/migration-to-v4). +Then, please follow [this guide](/docs/migration-to-v4). diff --git a/website/src/content/docs/docs/chartjs-v3.mdx b/website/src/content/docs/docs/chartjs-v3.mdx index f3790f30e..a3ad7b02a 100644 --- a/website/src/content/docs/docs/chartjs-v3.mdx +++ b/website/src/content/docs/docs/chartjs-v3.mdx @@ -35,7 +35,7 @@ Also, please consider upgrading your app to [Chart.js v4](#upgrading-to-chartjs- ## Upgrading to Chart.js v4 -First, upgrade packages. You'll need to install Chart.js v3 and the latest version of this library: +First, upgrade packages. You'll need to install Chart.js v4 and a compatible version of this library: @@ -61,4 +61,4 @@ First, upgrade packages. You'll need to install Chart.js v3 and the latest versi -Then, please follow the [this guide](/docs/migration-to-v5). +Then, please follow [this guide](/docs/migration-to-v5). diff --git a/website/src/content/docs/examples/area-chart.mdx b/website/src/content/docs/examples/area-chart.mdx index 93f667f59..be91a13e5 100644 --- a/website/src/content/docs/examples/area-chart.mdx +++ b/website/src/content/docs/examples/area-chart.mdx @@ -5,6 +5,6 @@ description: "Example of area chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of area chart in react-chartjs-2. +Example of area chart with the [``](/components/line/) component. diff --git a/website/src/content/docs/examples/bubble-chart.mdx b/website/src/content/docs/examples/bubble-chart.mdx index dd13d7486..63be582e4 100644 --- a/website/src/content/docs/examples/bubble-chart.mdx +++ b/website/src/content/docs/examples/bubble-chart.mdx @@ -5,6 +5,6 @@ description: "Example of bubble chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of bubble chart in react-chartjs-2. +Example of bubble chart with the [``](/components/bubble/) component. diff --git a/website/src/content/docs/examples/chart-events.mdx b/website/src/content/docs/examples/chart-events.mdx index d87c022cd..dc9950595 100644 --- a/website/src/content/docs/examples/chart-events.mdx +++ b/website/src/content/docs/examples/chart-events.mdx @@ -5,6 +5,6 @@ description: "Example of events in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of events in react-chartjs-2. +Example of events with the [``](/components/chart/) component. diff --git a/website/src/content/docs/examples/chart-ref.mdx b/website/src/content/docs/examples/chart-ref.mdx index 7bdef4357..c4fe07b0a 100644 --- a/website/src/content/docs/examples/chart-ref.mdx +++ b/website/src/content/docs/examples/chart-ref.mdx @@ -1,10 +1,12 @@ --- -title: "Chart Ref" +title: "Get Chart Ref" +sidebar: + label: "Chart Ref" description: "Example of getting ref in react-chartjs-2." --- import Example from '../../../components/Example.astro' -Example of getting ref in react-chartjs-2. +Example of getting ref with the [``](/components/chart/) component. diff --git a/website/src/content/docs/examples/doughnut-chart.mdx b/website/src/content/docs/examples/doughnut-chart.mdx index 5dd08af3a..4db2cd4fd 100644 --- a/website/src/content/docs/examples/doughnut-chart.mdx +++ b/website/src/content/docs/examples/doughnut-chart.mdx @@ -5,6 +5,6 @@ description: "Example of doughnut chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of doughnut chart in react-chartjs-2. +Example of doughnut chart with the [``](/components/doughnut/) component. diff --git a/website/src/content/docs/examples/gradient-chart.mdx b/website/src/content/docs/examples/gradient-chart.mdx index 0079ebf22..75b6a6691 100644 --- a/website/src/content/docs/examples/gradient-chart.mdx +++ b/website/src/content/docs/examples/gradient-chart.mdx @@ -1,10 +1,11 @@ --- title: "Gradient Chart" description: "Example of creating gradient chart in react-chartjs-2." +next: false --- import Example from '../../../components/Example.astro' -Example of creating gradient chart in react-chartjs-2. +Example of creating gradient chart with the [``](/components/chart/) component. diff --git a/website/src/content/docs/examples/grouped-bar-chart.mdx b/website/src/content/docs/examples/grouped-bar-chart.mdx index 3c8c5141c..7fae037aa 100644 --- a/website/src/content/docs/examples/grouped-bar-chart.mdx +++ b/website/src/content/docs/examples/grouped-bar-chart.mdx @@ -5,6 +5,6 @@ description: "Example of grouped bar chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of grouped bar chart in react-chartjs-2. +Example of grouped bar chart with the [``](/components/bar/) component. diff --git a/website/src/content/docs/examples/horizontal-bar-chart.mdx b/website/src/content/docs/examples/horizontal-bar-chart.mdx index e1e532f52..aa0cd8893 100644 --- a/website/src/content/docs/examples/horizontal-bar-chart.mdx +++ b/website/src/content/docs/examples/horizontal-bar-chart.mdx @@ -5,6 +5,6 @@ description: "Example of horizontal bar chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of horizontal bar chart in react-chartjs-2. +Example of horizontal bar chart with the [``](/components/bar/) component. diff --git a/website/src/content/docs/examples/index.md b/website/src/content/docs/examples/index.md index 7f6b04491..b59665019 100644 --- a/website/src/content/docs/examples/index.md +++ b/website/src/content/docs/examples/index.md @@ -1,6 +1,7 @@ --- title: "Examples" description: "List of react-chartjs-2 usage examples." +prev: false --- Every example is a small React app: see it running, read its code, or open it in StackBlitz to edit it. diff --git a/website/src/content/docs/examples/line-chart.mdx b/website/src/content/docs/examples/line-chart.mdx index 2f597af2f..17f4c241b 100644 --- a/website/src/content/docs/examples/line-chart.mdx +++ b/website/src/content/docs/examples/line-chart.mdx @@ -5,6 +5,6 @@ description: "Example of line chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of line chart in react-chartjs-2. +Example of line chart with the [``](/components/line/) component. diff --git a/website/src/content/docs/examples/multiaxis-line-chart.mdx b/website/src/content/docs/examples/multiaxis-line-chart.mdx index d34ac9f96..5430a0d3a 100644 --- a/website/src/content/docs/examples/multiaxis-line-chart.mdx +++ b/website/src/content/docs/examples/multiaxis-line-chart.mdx @@ -5,6 +5,6 @@ description: "Example of multiaxis line chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of multiaxis line chart in react-chartjs-2. +Example of multiaxis line chart with the [``](/components/line/) component. diff --git a/website/src/content/docs/examples/multitype-chart.mdx b/website/src/content/docs/examples/multitype-chart.mdx index 4b44ef3bc..09214bac2 100644 --- a/website/src/content/docs/examples/multitype-chart.mdx +++ b/website/src/content/docs/examples/multitype-chart.mdx @@ -5,6 +5,6 @@ description: "Example of multitype chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of multitype chart in react-chartjs-2. +Example of multitype chart with the [``](/components/chart/) component. diff --git a/website/src/content/docs/examples/pie-chart.mdx b/website/src/content/docs/examples/pie-chart.mdx index da2fd1b19..6339f6f1d 100644 --- a/website/src/content/docs/examples/pie-chart.mdx +++ b/website/src/content/docs/examples/pie-chart.mdx @@ -5,6 +5,6 @@ description: "Example of pie chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of pie chart in react-chartjs-2. +Example of pie chart with the [``](/components/pie/) component. diff --git a/website/src/content/docs/examples/polar-area-chart.mdx b/website/src/content/docs/examples/polar-area-chart.mdx index c3e4ed3a2..8b0a9ee10 100644 --- a/website/src/content/docs/examples/polar-area-chart.mdx +++ b/website/src/content/docs/examples/polar-area-chart.mdx @@ -5,6 +5,6 @@ description: "Example of polar area chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of polar area chart in react-chartjs-2. +Example of polar area chart with the [``](/components/polar-area/) component. diff --git a/website/src/content/docs/examples/radar-chart.mdx b/website/src/content/docs/examples/radar-chart.mdx index 1dd64bc50..ed581a2eb 100644 --- a/website/src/content/docs/examples/radar-chart.mdx +++ b/website/src/content/docs/examples/radar-chart.mdx @@ -5,6 +5,6 @@ description: "Example of radar chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of radar chart in react-chartjs-2. +Example of radar chart with the [``](/components/radar/) component. diff --git a/website/src/content/docs/examples/scatter-chart.mdx b/website/src/content/docs/examples/scatter-chart.mdx index 75079cfb5..a5b2aa647 100644 --- a/website/src/content/docs/examples/scatter-chart.mdx +++ b/website/src/content/docs/examples/scatter-chart.mdx @@ -5,6 +5,6 @@ description: "Example of scatter chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of scatter chart in react-chartjs-2. +Example of scatter chart with the [``](/components/scatter/) component. diff --git a/website/src/content/docs/examples/stacked-bar-chart.mdx b/website/src/content/docs/examples/stacked-bar-chart.mdx index 144f4bc21..0771c5194 100644 --- a/website/src/content/docs/examples/stacked-bar-chart.mdx +++ b/website/src/content/docs/examples/stacked-bar-chart.mdx @@ -5,6 +5,6 @@ description: "Example of stacked bar chart in react-chartjs-2." import Example from '../../../components/Example.astro' -Example of stacked bar chart in react-chartjs-2. +Example of stacked bar chart with the [``](/components/bar/) component. diff --git a/website/src/content/docs/examples/vertical-bar-chart.mdx b/website/src/content/docs/examples/vertical-bar-chart.mdx index db78781a4..e04a128e7 100644 --- a/website/src/content/docs/examples/vertical-bar-chart.mdx +++ b/website/src/content/docs/examples/vertical-bar-chart.mdx @@ -1,10 +1,11 @@ --- title: "Vertical Bar Chart" description: "Example of vertical bar chart in react-chartjs-2." +prev: Examples --- import Example from '../../../components/Example.astro' -Example of vertical bar chart in react-chartjs-2. +Example of vertical bar chart with the [``](/components/bar/) component. diff --git a/website/src/content/docs/faq/index.md b/website/src/content/docs/faq/index.md index 39c326882..dbb4685cf 100644 --- a/website/src/content/docs/faq/index.md +++ b/website/src/content/docs/faq/index.md @@ -1,6 +1,7 @@ --- title: "FAQ" description: "Frequently asked questions about react-chartjs-2." +prev: false --- Answers to the questions that come up most often. @@ -8,9 +9,11 @@ Answers to the questions that come up most often. - [Why doesn't the chart maintain its width/height?](/faq/maintain-aspect-ratio/) - [How to access the Chart.js instance?](/faq/chartjs-instance/) - [How to access the canvas context?](/faq/canvas-context/) -- [Why this library has "2" in its name?](/faq/why-two/) +- [Why does react-chartjs-2 have "2" in its name?](/faq/why-two/) - [Why is a background fill not working?](/faq/fill-property/) - [How to fix "... is not a registered element" error?](/faq/registered-element/) - [How to fix "... is not a registered scale" error?](/faq/registered-scale/) - [How to use react-chartjs-2 with TypeScript?](/faq/typescript/) - [How to fix "Cannot find module 'react-chartjs-2'" error?](/faq/esm-only/) + +Did not find the answer? [Ask a question in GitHub Discussions](https://github.com/reactchartjs/react-chartjs-2/discussions/categories/q-a). diff --git a/website/src/content/docs/faq/maintain-aspect-ratio.md b/website/src/content/docs/faq/maintain-aspect-ratio.md index b7ee5f929..18b91d05e 100644 --- a/website/src/content/docs/faq/maintain-aspect-ratio.md +++ b/website/src/content/docs/faq/maintain-aspect-ratio.md @@ -1,6 +1,7 @@ --- title: "Why doesn't the chart maintain its width/height?" description: "Make a react-chartjs-2 chart keep a custom width and height by setting the maintainAspectRatio option to false." +prev: FAQ --- In order for Chart.js to obey the custom size you need to set `maintainAspectRatio` to `false`: diff --git a/website/src/content/docs/index.mdx b/website/src/content/docs/index.mdx index 340e22aff..ad3886d5b 100644 --- a/website/src/content/docs/index.mdx +++ b/website/src/content/docs/index.mdx @@ -78,7 +78,7 @@ ChartJS.register(ArcElement, Tooltip, Legend); ``` -To learn more about importing and registering elements see [tree-shaking](https://react-chartjs-2.js.org/docs/migration-to-v4/#tree-shaking). +To learn more about importing and registering elements see [tree-shaking](/docs/migration-to-v4/#tree-shaking). ## Examples diff --git a/website/src/styles/global.css b/website/src/styles/global.css index 095be244b..0c94148db 100644 --- a/website/src/styles/global.css +++ b/website/src/styles/global.css @@ -11,8 +11,49 @@ --sl-color-accent-high: #1f1d5c; } -.rc2-ad { - margin-top: 1.5rem; +/* The ad sits at the bottom of the right sidebar, under the table of contents */ +@media (min-width: 72rem) { + .right-sidebar { + display: flex; + flex-direction: column; + } + + /* The right sidebar is as wide as the screen: the column is at least as wide as a sidebar */ + .rc2-bwndw-aside { + max-width: var(--sl-sidebar-width); + margin-top: auto; + padding-block: 1rem; + } +} + +/* Without the right sidebar, the ad follows the text of the page */ +.rc2-bwndw-main { + max-width: var(--sl-sidebar-width); + margin: 2rem auto 0; +} + +/* The ad already sets the footer apart from the text */ +.rc2-bwndw-main ~ footer .meta { + margin-top: 1rem; +} + +/* A long table of contents scrolls by itself, so the ad stays in view */ +@media (min-width: 72rem) and (min-height: 40rem) { + .right-sidebar-panel { + min-height: 0; + overflow-y: auto; + scrollbar-width: none; + } +} + +/* No bar for a table of contents that is not shown */ +:root:has(.rc2-no-toc) { + --sl-mobile-toc-height: 0rem; +} + +/* Long titles, such as the questions of the FAQ, fit the previous and next links */ +.pagination-links .link-title { + font-size: var(--sl-text-lg); } /* The logo sits beside the intro of the home page */ @@ -40,6 +81,11 @@ display: block; } +/* A block, so the example gets the gap above it like other content */ +rc2-example { + display: block; +} + .rc2-example-frame { width: 100%; height: 500px;