Skip to content

Commit

Permalink
feat(heading): add Heading and Section components
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Apr 21, 2024
1 parent 42e5026 commit 6806fa2
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 2 deletions.
51 changes: 50 additions & 1 deletion COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Component Index

> 166 components exported from [email protected].
> 168 components exported from [email protected].
## Components

Expand Down Expand Up @@ -63,6 +63,7 @@
- [`HeaderPanelLinks`](#headerpanellinks)
- [`HeaderSearch`](#headersearch)
- [`HeaderUtilities`](#headerutilities)
- [`Heading`](#heading)
- [`ImageLoader`](#imageloader)
- [`InlineLoading`](#inlineloading)
- [`InlineNotification`](#inlinenotification)
Expand Down Expand Up @@ -109,6 +110,7 @@
- [`Row`](#row)
- [`Search`](#search)
- [`SearchSkeleton`](#searchskeleton)
- [`Section`](#section)
- [`Select`](#select)
- [`SelectItem`](#selectitem)
- [`SelectItemGroup`](#selectitemgroup)
Expand Down Expand Up @@ -1852,6 +1854,28 @@ None.

None.

## `Heading`

### Types

```ts
export type SectionLevel = 1 | 2 | 3 | 4 | 5 | 6;
```

### Props

None.

### Slots

| Slot name | Default | Props | Fallback |
| :-------- | :------ | :---- | :------- |
| -- | Yes | -- | -- |

### Events

None.

## `ImageLoader`

### Props
Expand Down Expand Up @@ -3177,6 +3201,31 @@ None.
| mouseenter | forwarded | -- |
| mouseleave | forwarded | -- |

## `Section`

### Types

```ts
export type SectionLevel = 1 | 2 | 3 | 4 | 5 | 6;
```

### Props

| Prop name | Required | Kind | Reactive | Type | Default value | Description |
| :-------- | :------- | :--------------- | :------- | ---------------------------------------- | ---------------------- | ---------------------------------------------- |
| level | No | <code>let</code> | No | <code>SectionLevel</code> | <code>1</code> | Specify the level the section should start at. |
| tag | No | <code>let</code> | No | <code>keyof HTMLElementTagNameMap</code> | <code>"section"</code> | Specify the tag name |

### Slots

| Slot name | Default | Props | Fallback |
| :-------- | :------ | :---- | :------- |
| -- | Yes | -- | -- |

### Events

None.

## `Select`

### Props
Expand Down
57 changes: 56 additions & 1 deletion docs/src/COMPONENT_API.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total": 166,
"total": 168,
"components": [
{
"moduleName": "Accordion",
Expand Down Expand Up @@ -5386,6 +5386,21 @@
"events": [],
"typedefs": []
},
{
"moduleName": "Heading",
"filePath": "src/Heading/Heading.svelte",
"props": [],
"moduleExports": [],
"slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }],
"events": [],
"typedefs": [
{
"type": "1 | 2 | 3 | 4 | 5 | 6",
"name": "SectionLevel",
"ts": "type SectionLevel = 1 | 2 | 3 | 4 | 5 | 6"
}
]
},
{
"moduleName": "ImageLoader",
"filePath": "src/ImageLoader/ImageLoader.svelte",
Expand Down Expand Up @@ -10181,6 +10196,46 @@
"typedefs": [],
"rest_props": { "type": "Element", "name": "div" }
},
{
"moduleName": "Section",
"filePath": "src/Heading/Section.svelte",
"props": [
{
"name": "level",
"kind": "let",
"description": "Specify the level the section should start at.",
"type": "SectionLevel",
"value": "1",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "tag",
"kind": "let",
"description": "Specify the tag name",
"type": "keyof HTMLElementTagNameMap",
"value": "\"section\"",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
}
],
"moduleExports": [],
"slots": [{ "name": "__default__", "default": true, "slot_props": "{}" }],
"events": [],
"typedefs": [
{
"type": "1 | 2 | 3 | 4 | 5 | 6",
"name": "SectionLevel",
"ts": "type SectionLevel = 1 | 2 | 3 | 4 | 5 | 6"
}
]
},
{
"moduleName": "Select",
"filePath": "src/Select/Select.svelte",
Expand Down
72 changes: 72 additions & 0 deletions docs/src/pages/components/Heading.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
components: ["Heading", "Section"]
---

<script>
import { Heading, Section } from "carbon-components-svelte";
import Preview from "../../components/Preview.svelte";
</script>

The `Heading` component automatically adjusts the level of the heading based on the nesting level of the `Section` component.

## Default

For example, the top-level `Heading` inside a `Section` will be rendered as a semantic `h1` element.

<Section>
<Heading>Heading 1</Heading>
</Section>

## Nested sections

Section headings are capped at `h6`.

<Section>
<Heading>Heading 1</Heading>
<Section>
<Heading>Heading 2</Heading>
<Section>
<Heading>Heading 3</Heading>
<Section>
<Heading>Heading 4</Heading>
<Section>
<Heading>Heading 5</Heading>
<Section>
<Heading>Heading 6</Heading>
<Section>
<Heading>Capped at Heading 6</Heading>
</Section>
</Section>
</Section>
</Section>
</Section>
</Section>
<Section>
<Heading>Heading 2</Heading>
</Section>
</Section>


## Custom level

To specify a custom start level, use the `level` prop.

<Section level={5}>
<Heading>Starts at Heading 5</Heading>
<Section>
<Heading>Heading 6</Heading>
<Section>
<Heading>Capped at Heading 6</Heading>
</Section>
</Section>
</Section>

## Custom section element

The `Section` component renders a `section` by default.

Use the `tag` prop to render a different element.

<Section tag="div">
<Heading>Heading 1</Heading>
</Section>
18 changes: 18 additions & 0 deletions src/Heading/Heading.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
// @ts-check
/**
* @typedef {1 | 2 | 3 | 4 | 5 | 6} SectionLevel
*/
import { getContext } from "svelte";
/** @type {undefined | SectionLevel} */
const sectionLevel = getContext("Section");
$: tag = `h${sectionLevel ?? 1}`;
</script>
<svelte:element this="{tag}">
<slot />
</svelte:element>
42 changes: 42 additions & 0 deletions src/Heading/Section.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script>
// @ts-check
/**
* @typedef {1 | 2 | 3 | 4 | 5 | 6} SectionLevel
*/
/**
* Specify the level the section should start at.
* @type {SectionLevel}
*/
export let level = 1;
/**
* Specify the tag name
* @type {keyof HTMLElementTagNameMap}
*/
export let tag = "section";
import { getContext, setContext } from "svelte";
import { writable } from "svelte/store";
/** @type {undefined | SectionLevel} */
const parentLevel = getContext("Section");
/** @type {import ("svelte/store").Writable<SectionLevel>} */
const internalLevel = writable(level);
if (typeof parentLevel === "number") {
// @ts-expect-error
internalLevel.set(Math.min(parentLevel + 1, 6));
}
// Custom level should override the inferred parent level.
if (level !== 1) {
internalLevel.set(level);
}
setContext("Section", $internalLevel);
</script>

<svelte:element this="{tag}"><slot /></svelte:element>
2 changes: 2 additions & 0 deletions src/Heading/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Heading } from "./Heading.svelte";
export { default as Section } from "./Section.svelte";
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export { FormGroup } from "./FormGroup";
export { FormItem } from "./FormItem";
export { FormLabel } from "./FormLabel";
export { Grid, Row, Column } from "./Grid";
export { Heading, Section } from "./Heading";
export { ImageLoader } from "./ImageLoader";
export { InlineLoading } from "./InlineLoading";
export { Layer } from "./Layer";
Expand Down
10 changes: 10 additions & 0 deletions tests/Heading.test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
import { Heading, Section } from "../types";
</script>

<Section tag="div">
<Heading>Heading 1</Heading>
<Section level="{5}">
<Heading>Starts at Heading 5</Heading>
</Section>
</Section>
11 changes: 11 additions & 0 deletions types/Heading/Heading.svelte.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { SvelteComponentTyped } from "svelte";

export type SectionLevel = 1 | 2 | 3 | 4 | 5 | 6;

export interface HeadingProps {}

export default class Heading extends SvelteComponentTyped<
HeadingProps,
Record<string, any>,
{ default: {} }
> {}
23 changes: 23 additions & 0 deletions types/Heading/Section.svelte.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { SvelteComponentTyped } from "svelte";

export type SectionLevel = 1 | 2 | 3 | 4 | 5 | 6;

export interface SectionProps {
/**
* Specify the level the section should start at.
* @default 1
*/
level?: SectionLevel;

/**
* Specify the tag name
* @default "section"
*/
tag?: keyof HTMLElementTagNameMap;
}

export default class Section extends SvelteComponentTyped<
SectionProps,
Record<string, any>,
{ default: {} }
> {}
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export { default as FormLabel } from "./FormLabel/FormLabel.svelte";
export { default as Grid } from "./Grid/Grid.svelte";
export { default as Row } from "./Grid/Row.svelte";
export { default as Column } from "./Grid/Column.svelte";
export { default as Heading } from "./Heading/Heading.svelte";
export { default as Section } from "./Heading/Section.svelte";
export { default as ImageLoader } from "./ImageLoader/ImageLoader.svelte";
export { default as InlineLoading } from "./InlineLoading/InlineLoading.svelte";
export { default as Layer } from "./Layer/Layer.svelte";
Expand Down

0 comments on commit 6806fa2

Please sign in to comment.