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

[docs] Fix layout shift example page #4350

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 5 additions & 9 deletions docs/src/modules/components/ExamplesGrid/ExamplesGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ function CodeSandboxIcon() {
}

function Templates({ examplesFile }: TemplatesProps) {
const [examples, setExamples] = React.useState<Example[]>([]);

React.useEffect(() => {
const importExamples = async () => {
const exampleContent = await import(`./${examplesFile}`);
setExamples(exampleContent.default);
};
importExamples();
}, [examplesFile]);
// @ts-ignore
const req = require.context('./', false);
Copy link
Member

@Janpot Janpot Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will pull the whole folder (including files we don't need such as this source file) in the bundle. I'd prefer to just import them statically instead of doing proprietary webpack stuff. It also makes it type safe automatically.

import coreExamples from './core-examples'
import studioExamples from './studio-examples'

const EXAMPLES_BY_PRODUCT = {
  'core-examples': coreExamples,
  'studio-examples': studioExamples
}

// ...

const examples = EXAMPLES_BY_PRODUCT[examplesFile]

Or better yet, avoid the indirection, just pass the array as a prop and import the correct file on the callsite.

// TODO: When migrating away from webpack, or when we have time, to create dedicate files for each
// demo. We shouldn't do those dynamic imports in the first place ⬇️
const examples = req(`./${examplesFile}`).default() as Example[];
const docsTheme = useTheme();

return (
Expand Down
Loading