Skip to content

RFC: Standardize shorthand prop names for primary content #391

@levithomason

Description

@levithomason

Issue

Many components provide a shorthand prop for defining the primary content of the component. We currently have inconsistent prop names for this shorthand prop. It makes it confusing as a developer to have to use text in some places, content in others, and more.

Additionally, the text prop (and others) are already used in some components to control features. We need a standard that is consistent and avoids collisions.

Proposal

Here are the proposed rules for choosing the primary content shorthand prop name:

Naming

1. Use the component part name

If there is a subcomponent which holds the primary content of the component, then the subcomponent name should be used for the shorthand prop name:

<Step description='...' />
<Step.Description>...</Step.Description>

This makes intuitive sense as you are passing down a prop from the parent to the subcomponent with the same name. It also follows what we are doing with the icon and image props, where you can configure the children with the same prop name.

It allows us to also add standardized feature support for various component parts via our factories, createDescription(...), createMeta(...). Contrived examples, we could standardize props like childKey, elementType, or rending links, etc.

2. Use content

If the component does not have a subcomponent name to use, then use content:

<Step.Description content='...' />   // is a subcomponent (no further subcomponents)
<Label content='...' />              // has no subcomponent

This makes sense in the context of SUI as "content" is a standard component part that means "the primary content of the component". If there are no child components left to configure, then you are adding the "content" of the component.

Again, this would allow us to createContent(content) and add standardized support via our factories if we wanted.

A good counter argument to content on a component that has no subcomponents is, "Why not just use children?" This is already built into React of course.

<Step.Description children='...' />
<Label children='...' />

PropTypes

PropType validation has also come up in https://github.com/TechnologyAdvice/stardust/pull/390#discussion_r74690941. Initially we thought to limit these to strings only. This allows the props to be used as child keys in arrays. It also simplifies things for the user.

However, some components like <Statistic.Value /> may very well accept a number on <Statistic value={100} />. It would be troublesome to force users to convert numbers to strings in this case. Also, numbers do not present the same issues for child keys as accepting objects.

customPropTypes.shorthand

I propose we create this propType to consistently validate shorthand props. Not only would it standardize shorthand props, we could then bake in disallow(['children']) as well. This would pair very nicely with standardize factories for shorthand.

CONTRIBUTING

Any PR resolving this issue should update the CONTRIBUTING.md with the final shorthand props spec.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions