Skip to main content

Extend Section component

This guided example shows how you'd add components to your Design System that use a kickstartDS base component pretty directly. But unlike just adapting a component, extending a component also involves adding something to it, or changing the way certain things work under the hood, by composing multiple kickstartDS base components (whereas customizing a component does that by adding changes to the customized components React template). This expands possible applications of existing kickstartDS components greatly.

Even while using the component rather directly from kickstartDS, you'll want to find the correct set of properties for your own use case. Components in kickstartDS come equipped with properties for a wide range of possible use cases, so it makes sense to reduce those to the ones you really need... to make components easier to understand, use and reason about!

We call this type of workflow Extension. Learn more about it in our dedicated guide about it. If you're unsure about something, have a look over there. We go into more background detail there about what we're doing here.

Not touching the actual markup generated by components let's us get by without adding any custom styling (CSS / SCSS) to it. We simply reuse the already existing Design Token and component structure.

Overview

This is how the result of this guide will look like:

It will only need two simple steps for that:

  1. Component Definition, and
  2. Component Creation

For more details about those steps, have a look at the guide about different component processes and their shared structure.

Requirements

This guide assumes that you already have a working Design System, that is based on kickstartDS, running.
If that's not the case, follow our Create your Design System guide.

1. Component Definition

Purpose

We've found that we'd love to use the existing kickstartDS Section, but it's not quite flexible enough for our taste. We'd like to add more call-to-actions to our page, which mostly consists of such Sections, and don't need as much flexibility for the included headline.

We take width, gutter, mode, content, spaceBefore, spaceAfter and inverted directly from the Section, and rename background to style for our version of it. And crucially we add our own property ctas, to hold call-to-actions, into the mix, while reducing the complexity of headline significantly... from mapping to all of the Headline properties, to just a single string type prop setting its content.

We also keep the name Section, as it fits our use case well enough already.

Structure

Defining the structure of a component means finding the component API for it:

PropertyTypeDescription
headlinestringHeadline for the section
widthenumWidth of section to use
gutterenumSize of gutter to use
modeenumLayout mode used for section contents
contentarrayAllowed content for the section
styleenumStyle of section, influences background
spaceBeforeenumAmount of spacing before the section
spaceAfterenumAmount of spacing after the section
invertedbooleanWhether to invert the section
ctasarrayCall-to-actions to show
ctas[].label *stringLabel for the Call to action
ctas[].target *stringTarget for the Call to action

Fields that should be required are marked with a *.

While directly helping us get a better grasp on our new component, these will also be used to write our JSON Schema later!

2. Component Creation

We like to colocate components. This means to have all involved files next to each other in the same folder; the template (.jsx / .tsx), potential CSS / SASS (.css / .scss), JavaScript (.js / .ts), our JSON Schema component definition (.schema.json), and so on.

So we start by creating the directory src/components/section, from our Design System repository root:

mkdir -p src/components/section

This is the folder we'll add new files to in the coming few paragraphs.

JSON Schema definition

First file we'll create is the JSON Schema definition, encoding the structure we've defined for our component before:

Finished JSON Schema

We'll work our way up to this JSON Schema definition.

Start with just the boilerplate for a component definition

This includes all necessarily required values for a valid component definition in kickstartDS.

Add basic info describing component

We start by adding a title, description and $id attribute. The correct $id depends on your Design System configuration. We'll assume you've created components before, living under the schema prefix http://schema.mydesignsystem.com.

Create headline field

The headline field is a straight-forward string type properties, so we just document it a bit!
We do mark it by setting format to markdown, though, to enable some light RTE-like formatting options of the rendered text later on.

Add allowed widths 1/2

We add a width property of type string...

Add allowed widths 2/2

... and make it an enum, defining its available options explicitly. We also set a default.

Add allowed gutters 1/2

We add a gutter property of type string...

Add allowed gutters 2/2

... and make it an enum, defining its available options explicitly. We also set a default.

Add allowed modes 1/2

We add a mode property of type string...

Add allowed modes 2/2

... and make it an enum, defining its available options explicitly. We also set a default.

Add allowed contents

We add a field content of type array´. Let's assume we have three components that can be used as content for the section. We reference each one using anyOfand$ref`.

Add allowed styles 1/2

We add a style property of type string...

Add allowed styles 2/2

... and make it an enum, defining its available options explicitly. We also set a default.

Add allowed spaceBefores 1/2

We add a spaceBefore property of type string...

Add allowed spaceBefores 2/2

... and make it an enum, defining its available options explicitly. We also set a default.

Add allowed spaceAfters 1/2

We add a spaceAfter property of type string...

Add allowed spaceAfters 2/2

... and make it an enum, defining its available options explicitly. We also set a default.

Add inverted property

We add a property inverted, as a boolean.

Add ctas property 1/2

We add the ctas property of type array, this will hold our call-to-action related properties label and target.

Add ctas property 2/2

We specify the array items type, adding an object with properties label and target to it, both of simple string types (with target having format set to uri to enable resource-like behaviour).

Finished JSON Schema

Let's have a look at our completed JSON Schema definition.

src/components/section/section.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://schema.mydesignsystem.com/section.schema.json",
"title": "Section",
"description": "Component used to layout components into pages",
"type": "object",
"properties": {
"headline": {
"type": "string",
"title": "Headline",
"description": "Headline for the section",
"format": "markdown"
},
"width": {
"type": "string",
"title": "Width",
"description": "Width of section to use",
"enum": ["full", "max", "wide", "default", "narrow"],
"default": "default"
},
"gutter": {
"type": "string",
"title": "Gutter",
"description": "Size of gutter to use",
"enum": ["large", "default", "small", "none"],
"default": "default"
},
"mode": {
"type": "string",
"title": "Mode",
"description": "Layout mode used for section contents",
"enum": ["default", "tile", "list"],
"default": "default"
},
"content": {
"type": "array",
"title": "Content",
"description": "Allowed content for the section",
"items": {
"anyOf": [
{
"$ref": "http://schema.mydesignsystem.com/button.schema.json"
},
{
"$ref": "http://schema.mydesignsystem.com/headline.schema.json"
},
{
"$ref": "http://schema.mydesignsystem.com/teaser-card.schema.json"
}
]
}
},
"style": {
"type": "string",
"title": "Style",
"description": "Style of background",
"enum": ["default", "accent", "bold"],
"default": "default"
},
"spaceBefore": {
"type": "string",
"title": "Space Before",
"description": "Amount of spacing before the section",
"enum": ["default", "small", "none"],
"default": "default"
},
"spaceAfter": {
"type": "string",
"title": "Space After",
"description": "Amount of spacing after the section",
"enum": ["default", "small", "none"],
"default": "default"
},
"inverted": {
"type": "boolean",
"title": "Inverted",
"description": "Whether to invert the section",
"default": false
},
"ctas": {
"type": "array",
"title": "Call to actions",
"description": "Add Call to actions to the end of the section",
"items": {
"type": "object",
"properties": {
"label": {
"type": "string",
"title": "Label",
"description": "Label for the Call to action"
},
"target": {
"type": "string",
"title": "Target",
"description": "Target for the Call to action",
"format": "uri"
}
},
"required": ["label", "target"]
}
}
},
"required": []
}

This concludes creating the JSON Schema. When running the schema generation in our Design System again, we should now automatically end up with a corresponding type definition to be used in creation of the template in the next step:

src/components/section/SectionProps.ts
src/components/section/section.schema.json
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run `yarn run schema` to regenerate this file.
*/

/\*\*

- Headline for the section
\*/
export type Headline = string;
/\*\*
- Width of section to use
\*/
export type Width = "full" | "max" | "wide" | "default" | "narrow";
/\*\*
- Size of gutter to use
\*/
export type Gutter = "large" | "default" | "small" | "none";
/\*\*
- Layout mode used for section contents
\*/
export type Mode = "default" | "tile" | "list";
/\*\*
- Text content to display inside the button
\*/
export type Label = string;
/\*\*
- Target that should be linked, makes the button behave like a link semantically
\*/
export type Target = string;
/\*\*
- Variant of button to be used
\*/
export type Variant = "primary" | "secondary" | "tertiary";
/\*\*
- Size of button to use
\*/
export type Size = "small" | "medium" | "large";
/\*\*
- Whether the button should be disabled
\*/
export type Disabled = boolean;
/\*\*
- Text content of headline
\*/
export type Text = string;
/\*\*
- Subheadline content
\*/
export type Sub = string;
/\*\*
- Switch order of headline and subheadline
\*/
export type SwitchOrder = boolean;
/\*\*
- Level of headline to use
\*/
export type Level = "h1" | "h2" | "h3" | "h4" | "p";
/\*\*
- Style of headline to show
\*/
export type Style = "h1" | "h2" | "h3" | "h4" | "p";
/\*\*
- Whether to display space after headline
\*/
export type SpaceAfter = "minimum" | "small" | "large";
/\*\*
- Headline for the teaser card
\*/
export type Headline2 = string;
/\*\*
- Body text for the teaser card
\*/
export type Text1 = string;
/\*\*
- Target that should be linked
\*/
export type Target1 = string;
/\*\*
- Image to display as cover
\*/
export type Image = string;
/\*\*
- Whether to invert the card
\*/
export type InvertCard = boolean;
/\*\*
- Allowed content for the section
\*/
export type Content = (Button | Headline1 | TeaserCard)[];
/\*\*
- Style of background
\*/
export type Style1 = "default" | "accent" | "bold";
/\*\*
- Amount of spacing before the section
\*/
export type SpaceBefore = "default" | "small" | "none";
/\*\*
- Amount of spacing after the section
\*/
export type SpaceAfter1 = "default" | "small" | "none";
/\*\*
- Whether to invert the section
\*/
export type Inverted = boolean;
/\*\*
- Label for the Call to action
\*/
export type Label1 = string;
/\*\*
- Target for the Call to action
\*/
export type Target2 = string;
/\*\*
- Add Call to actions to the end of the section
\*/
export type CallToActions = {
label: Label1;
target: Target2;
[k: string]: unknown;
}[];

/\*\*

- Component used to layout components into pages
\*/
export interface SectionProps {
headline?: Headline;
width?: Width;
gutter?: Gutter;
mode?: Mode;
content?: Content;
style?: Style1;
spaceBefore?: SpaceBefore;
spaceAfter?: SpaceAfter1;
inverted?: Inverted;
ctas?: CallToActions;
[k: string]: unknown;
}
/\*\*
- Component used for user interaction
\*/
export interface Button {
label: Label;
target?: Target;
variant?: Variant;
size?: Size;
disabled?: Disabled;
[k: string]: unknown;
}
/\*\*
- Component used for headlines
\*/
export interface Headline1 {
text: Text;
sub?: Sub;
switchOrder?: SwitchOrder;
level: Level;
style?: Style;
spaceAfter?: SpaceAfter;
[k: string]: unknown;
}
/\*\*
- Component used to tease content
\*/
export interface TeaserCard {
headline: Headline2;
text: Text1;
target: Target1;
image?: Image;
inverted?: InvertCard;
[k: string]: unknown;
}

How your schema generation is started might change depending on your setup. If you've followed our "Create your Design System" guide before, or want to add it like we do, follow this section of it closely.

React template

As the final step for this example, we'll add the template. This will be a purely functional React component, mapping our component structure (as defined in the JSON Schema) to the original component we're basing our work off of; the kickstartDS Storytelling component.

Finished React template

We'll work our way up to this React template.

Start with a boilerplate

Again we'll start with a very basic skeleton for our React component. We're using TypeScript here (.tsx), but it works the same with plain JSX (.jsx).

Add correct typings

Import and add generated props from SectionProps.ts. Generated by our JSON Schema, these guarantee you're matching your expected component structure while implementing. In combination with TypeScript this enables auto-complete and auto-fix for even better DX! (see here, at the very end of that section, for more details)

We also add HTMLAttributes<HTMLElement> to the type signature for the props that we'll pass through to the native HTML element underneath.

Destructure props

We also need to add our own properties, so we'll destructure props. We add our default values here, too. We'll just pass through everything HTMLAttributes related!

Add Section component 1/7

Now we'll import and add the kickstartDS Section component. To start, we'll use the hard-coded properties of the Content Boxes Section variant from our kickstartDS Design System.

We'll omit the child components added there (ContentBoxes), as they only exist for illustrative purposes inside that Storybook. We will just pass children through to the original Section, later.

Add Section component 2/7

We remove all of the unneeded stuff, as there are a bunch of properties that are completely optional, mainly those having their values undefined or null in the copied JSX, or ones which just state the default value of that property anyway. Those can be freely removed.

Add Section component 3/7

We now import the component we want to use to display the call-to-actions, in this case a Button included in our own Design System. We also add a second Section (losing its headline) to hold our Buttons, and connect those to cta.label. We choose the variant of Button by index, first equals primary, second means secondary and all others are tertiary.

Add Section component 4/7

We then connect the props as defined in our component API that are directly taken from the underlying kickstartDS base component by just passing them through. We also destructure props first, so our own properties take precedence when set. We add it to the first Section, that will always be rendered.

Add Section component 5/7

We renamed background to style in our component API, so we add that in its renamed form... to both Sections.

Add Section component 6/7

We connect our property headline to the first Sections headline.content.

Add Section component 7/7

As a final step, we make sure the spacing between the two Sections is optimized, so it will look seamless later on. We also hard code some options of the second Section.

Add component Provider

We want our Section to replace all default kickstartDS base Sections, no matter where they appear. We add a Provider for that purpose now!

Finished React template

Let's have a look at our completed React template.

src/components/section/SectionComponent.tsx
import { HTMLAttributes, FC, PropsWithChildren } from "react";

import {
SectionContextDefault,
SectionContext,
} from "@kickstartds/base/lib/section";

import { SectionProps } from "./SectionProps";
import { Button } from "../button/ButtonComponent";

export const Section: FC<
SectionProps & HTMLAttributes<HTMLElement>
> = ({
headline,
width = "default",
gutter = "default",
mode = "default",
style = "default",
spaceBefore = "default",
spaceAfter = "default",
inverted = false,
ctas = [],
...props
}) => {
return (
<>
<SectionContextDefault
{...props}
background={style}
headline={{
content: headline
}}
width={width}
gutter={gutter}
mode={mode}
spaceBefore={spaceBefore}
spaceAfter={ctas && ctas.length > 0 ? 'none' : spaceAfter}
inverted={inverted}
/>
{ctas && ctas.length > 0 && (
<SectionContextDefault
background={style}
width="narrow"
gutter={gutter}
mode="default"
spaceBefore="default"
spaceAfter={spaceAfter}
inverted={inverted}
>
{ctas.map((cta, index) => (
<Button label={cta.label} variant={index === 0 ? "primary" : index === 1 ? "secondary": "tertiary" } size="medium" />
))}
</SectionContextDefault>
)}
</>
);
};

export const SectionProvider: FC<PropsWithChildren<any>> = (props) => (
<SectionContext.Provider {...props} value={Section} />
);

To complete the template we add the SectionProvider to our src/components/Providers.jsx:

src/components/Providers.jsx
import { ButtonProvider } from "./button/ButtonComponent";
import { SectionProvider } from "./section/SectionComponent";
import { TeaserBoxProvider } from "./teaser-card/TeaserCardComponent";
import { HeadlineProvider } from "./headline/HeadlineComponent";

export default (props) => (
<ButtonProvider>
<HeadlineProvider>
<SectionProvider>
<TeaserBoxProvider {...props} />
</SectionProvider>
</HeadlineProvider>
</ButtonProvider>
);

This concludes the creation of our new Section component. It's now ready to be used inside your Design System, and available to your down stream consumers... hopefully efficiently closing a gap for them!

Use of Storybook

If you're using Storybook, you can follow this part of the example to get all the integration goodness possible with kickstartDS!

Storybook setup

This guide assumes you're using a set up like described in our Create your Design System guide! Be sure to adapt commands and configuration to your use accordingly, when following this part!

Add the following file to your src/components/section folder:

Import re-usable Section Stories

Import Section component and add it to the Template that we'll bind Stories to.

Import re-usable TeaserCard Stories

Import TeaserCard Stories as demo content for our Section Story. Add them to the Template, too.

Import Storybook Controls helpers

Import dereferenced component JSON Schema and getArgsShared helper to generate Storybook Controls, and parameterize Storybook JSON Schema Addon.

Overwrite Story values where needed

We set all the Story defaults specific to our component.

Convert args to flat keys

We use pack to convert all deep JSON args to flat (. delimited) keys and values. This is the format your Storybook Controls get generated off.

Create Section variants

We do this by binding to our Template, and use pack to convert all deep JSON args to flat (. delimited) keys and values. This is the format your Storybook Controls get generated off.

src/components/section/Section.stories.jsx
import merge from "deepmerge";
import {
pack,
unpack,
getArgsShared,
} from "@kickstartds/core/lib/storybook";
import sectionStories from "@kickstartds/base/lib/section/section.stories";
import TeaserCardStory, {
CardWithImage,
} from "../teaser-card/TeaserCard.stories";
import schema from "./section.schema.dereffed.json";

const Section = sectionStories.component;
CardWithImage.displayName = "Teaser Card";
const Template = (args) => {
return (
<Section {...args}>
<CardWithImage
{...merge(TeaserCardStory.args, unpack(CardWithImage.args))}
/>
<CardWithImage
{...merge(TeaserCardStory.args, unpack(CardWithImage.args))}
/>
<CardWithImage
{...merge(TeaserCardStory.args, unpack(CardWithImage.args))}
/>
</Section>
)
};

const { args, argTypes } = getArgsShared(schema);

export default {
...sectionStories,
title: "Layout/Section",
args,
argTypes,
parameters: {
jsonschema: schema,
},
};

export const TeaserCards = Template.bind({});
TeaserCards.args = pack({
headline: 'Section headline',
mode: "tile",
ctas: [],
});

export const WithCtas = Template.bind({});
WithCtas.args = pack({
headline: 'Section headline',
mode: "tile",
ctas: [{
label: 'Section CTA 1',
target: '#',
}, {
label: 'Section CTA 2',
target: '#',
}, {
label: 'Section CTA 3',
target: '#',
}
]
});

If you reopen your Storybook now, or if you kept it running while following along, you should now see your new Section in all its glory!

Finished Code Sandbox

You can also have a look at the completed component in the following Code Sandbox:

Toggle the file browser with the hamburger icon at the top left, or open it directly in your browser.