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

Feature : Components Documentation for "rest props" #68099

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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 bin/api-docs/gen-components-docs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ await Promise.all(
const docs = generateMarkdownDocs( {
typeDocs,
subcomponentTypeDocs,
additionalProps: manifest.additionalProps,
} );
const outputFile = path.resolve(
path.dirname( manifestPath ),
Expand Down
8 changes: 6 additions & 2 deletions bin/api-docs/gen-components-docs/markdown/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ function normalizeTrailingNewline( str ) {
return str.replace( /\n*$/, '\n' );
}

export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {
export function generateMarkdownDocs( {
typeDocs,
subcomponentTypeDocs,
additionalProps,
} ) {
const mainDocsJson = [
{ h1: typeDocs.displayName },
'<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->',
{
p: `<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-${ typeDocs.displayName.toLowerCase() }--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>`,
},
normalizeTrailingNewline( typeDocs.description ),
...generateMarkdownPropsJson( typeDocs.props ),
...generateMarkdownPropsJson( typeDocs.props, {}, additionalProps ),
];

const subcomponentDocsJson = subcomponentTypeDocs?.length
Expand Down
13 changes: 12 additions & 1 deletion bin/api-docs/gen-components-docs/markdown/props.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ function renderPropType( type ) {
}
}

export function generateMarkdownPropsJson( props, { headingLevel = 2 } = {} ) {
export function generateMarkdownPropsJson(
props,
{ headingLevel = 2 } = {},
additionalProps = false
) {
const sortedKeys = Object.keys( props ).sort( ( [ a ], [ b ] ) =>
a.localeCompare( b )
);
Expand Down Expand Up @@ -46,5 +50,12 @@ export function generateMarkdownPropsJson( props, { headingLevel = 2 } = {} ) {
} )
.filter( Boolean );

if ( additionalProps ) {
propsJson.push( [
{ [ `h${ headingLevel + 1 }` ]: `\`Additional props\`` },
`All other props will be passed directly to the underlying \`${ additionalProps }\` element.`,
] );
}

return [ { [ `h${ headingLevel }` ]: 'Props' }, ...propsJson ];
}
5 changes: 5 additions & 0 deletions packages/components/schemas/docs-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
},
"required": [ "displayName", "filePath" ]
}
},
"additionalProps": {
"type": "string",
"default": false,
"description": "The wrapper associated with the component which accepts additional props."
}
},
"required": [ "displayName", "filePath" ]
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/badge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ Badge variant.
- Type: `"default" | "info" | "success" | "warning" | "error"`
- Required: No
- Default: `default`

### `Additional props`

All other props will be passed directly to the underlying `span` element.
3 changes: 2 additions & 1 deletion packages/components/src/badge/docs-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "../../schemas/docs-manifest.json",
"displayName": "Badge",
"filePath": "./index.tsx"
"filePath": "./index.tsx",
"additionalProps": "span"
}
8 changes: 8 additions & 0 deletions packages/components/src/badge/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const meta = {
title: 'Components/Containers/Badge',
id: 'components-badge',
tags: [ 'status-private' ],
argTypes: {
// @ts-expect-error
'': {
control: false,
table: { category: 'Other' },
description: 'This component also accepts props of `span`',
},
},
} satisfies Meta< typeof Badge >;

export default meta;
Expand Down
Loading