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

Allow dynamically loaded example files #1558

Merged
merged 1 commit into from
May 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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN rm -r /usr/share/nginx/html && rm /etc/nginx/conf.d/default.conf
COPY --from=builder /frontend-shared/build /usr/share/nginx/html
COPY ./templates/index.html /usr/share/nginx/html/index.html
COPY ./images /usr/share/nginx/html/images
COPY ./src/pattern-library/examples /usr/share/nginx/html/examples
acelaya marked this conversation as resolved.
Show resolved Hide resolved
COPY conf/nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 5001
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ import { Link } from '@hypothesis/frontend-shared';
- [Development guide](docs/developing.md)
- [Release guide](docs/releases.md)
- [Adding examples](docs/examples.md)
28 changes: 28 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Adding code examples

Component library documentation frequently needs to show interactive examples, along with the code for them.

Manually writing those code snippets has some issues: they are not covered by your type checking and linting tasks, and they can accidentally get outdated.

The web-based documentation included with this library allows to create example files which are both used as regular modules that can be imported for interactive examples, but also read as plain text to generate their corresponding code snippet.

These files are type-checked, formatted and linted like any other source files, and the code snippet will always be in sync with the interactive example.

## Using example files

When you want to create a code example, add a file with the name of your choice inside `src/pattern-library/examples` directory.

You can then reference this file from the web-based pattern library, passing the `exampleFile` prop to the `<Library.Demo />` component.

```tsx
<Library.Demo exampleFile="some-example-file-name" />
```

## Considerations

There are some considerations and limitations when working with example files.

- They all need to have the `tsx` extension.
- Nested directories are not supported, so it's a good idea to add contextual prefixes to example files. For example, all files related with `SelectNext` can be prefixed with `select-next` to make sure they are all grouped together.
- Example files need to have a Preact component as their default export.
- When generating the source code snippet, import statements are stripped out, to avoid internal module references which are not relevant for the library consumers.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@hypothesis/frontend-testing": "^1.2.2",
"@rollup/plugin-babel": "^6.0.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
acelaya marked this conversation as resolved.
Show resolved Hide resolved
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-virtual": "^3.0.0",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { babel } from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { string } from 'rollup-plugin-string';

Expand Down Expand Up @@ -27,6 +28,7 @@ function bundleConfig(name, entryFile) {
exclude: 'node_modules/**',
extensions: ['.js', '.ts', '.tsx'],
}),
dynamicImportVars(),
nodeResolve({ extensions: ['.js', '.ts', '.tsx'] }),
commonjs({ include: 'node_modules/**' }),
string({
Expand Down
4 changes: 4 additions & 0 deletions scripts/serve-pattern-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function servePatternLibrary(port = 4001) {
app.use('/scripts', express.static(path.join(dirname, '../build/scripts')));
app.use('/styles', express.static(path.join(dirname, '../build/styles')));
app.use('/images', express.static(path.join(dirname, '../images')));
app.use(
'/examples',
express.static(path.join(dirname, '../src/pattern-library/examples')),
);
acelaya marked this conversation as resolved.
Show resolved Hide resolved

// For any other path, serve the index.html file to allow client-side routing
app.get('/:path?', (req, res) => {
Expand Down
Loading