Remove additional exports of breakpoint symbols.

Restore typedef entries in `Breakpoint` component.
Generate type definitions for `constants.js` and `breakpointObserver.js`.
Validate size passed to `smallerThan`/`largerThan`.
Document `breakpointObserver`/`breakpoints`.
Fix `Breakpoint` component docs/example.
This commit is contained in:
Harald Brunner 2022-02-20 16:50:22 +01:00
commit 213d4debe7
13 changed files with 97 additions and 14 deletions

View file

@ -348,6 +348,14 @@ None.
## `Breakpoint` ## `Breakpoint`
### Types
```ts
export type BreakpointSize = "sm" | "md" | "lg" | "xlg" | "max";
export type BreakpointValue = 320 | 672 | 1056 | 1312 | 1584;
```
### Props ### Props
| Prop name | Kind | Reactive | Type | Default value | Description | | Prop name | Kind | Reactive | Type | Default value | Description |

View file

@ -403,7 +403,18 @@
"detail": "{ size: BreakpointSize; breakpointValue: BreakpointValue; }" "detail": "{ size: BreakpointSize; breakpointValue: BreakpointValue; }"
} }
], ],
"typedefs": [] "typedefs": [
{
"type": "\"sm\" | \"md\" | \"lg\" | \"xlg\" | \"max\"",
"name": "BreakpointSize",
"ts": "type BreakpointSize = \"sm\" | \"md\" | \"lg\" | \"xlg\" | \"max\""
},
{
"type": "320 | 672 | 1056 | 1312 | 1584",
"name": "BreakpointValue",
"ts": "type BreakpointValue = 320 | 672 | 1056 | 1312 | 1584"
}
]
}, },
{ {
"moduleName": "Button", "moduleName": "Button",

View file

@ -22,6 +22,14 @@ This utility component uses the [Window.matchMedia API](https://developer.mozill
Bind to the `size` prop to determine the current breakpoint size. Possible values include: `"sm" | "md" | "lg" | "xlg" | "max"`. Bind to the `size` prop to determine the current breakpoint size. Possible values include: `"sm" | "md" | "lg" | "xlg" | "max"`.
The `"on:match"` event will fire only when a breakpoint change event occurs (e.g., the browser width is resized). The `"on:change"` event will fire when the size is initially determined and when a breakpoint change event occurs (e.g., the browser width is resized).
<FileSource src="/framed/Breakpoint/Breakpoint" /> <FileSource src="/framed/Breakpoint/Breakpoint" />
### Store and Breakpoint Values
As an alternative to the component, `breakpointObserver` can be used to get the current size as a Svelte store. The store has two additional functions which create derived stores that return a `boolean` indicating whether the size is smaller/larger than a certain breakpoint.
There also exists a `breakpoints` dictionary mapping from `BreakpointSize` to `BreakpointValue`.
<FileSource src="/framed/Breakpoint/BreakpointObserver" />

View file

@ -5,13 +5,13 @@
let events = []; let events = [];
</script> </script>
<Breakpoint bind:size on:match="{(e) => (events = [...events, e.detail])}" /> <Breakpoint bind:size on:change="{(e) => (events = [...events, e.detail])}" />
<p>Resize the width of your browser.</p> <p>Resize the width of your browser.</p>
<h6>Breakpoint size</h6> <h6>Breakpoint size</h6>
<h1>{size}</h1> <h1>{size}</h1>
<h6>on:match</h6> <h6>on:change</h6>
<pre> <pre>
{JSON.stringify(events, null, 2)} {JSON.stringify(events, null, 2)}
</pre> </pre>

View file

@ -0,0 +1,15 @@
<script>
import {
breakpointObserver,
breakpoints,
} from "carbon-components-svelte/src/Breakpoint";
const size = breakpointObserver();
const smaller = size.smallerThan("md");
const larger = size.largerThan("md");
</script>
<p>Current breakpoint size: {$size}</p>
<p>Current breakpoint value: {breakpoints[$size]}px</p>
<p>Smaller than medium: {$smaller}</p>
<p>Larger than medium: {$larger}</p>

View file

@ -1,5 +1,7 @@
<script> <script>
/** /**
* @typedef {"sm" | "md" | "lg" | "xlg" | "max"} BreakpointSize
* @typedef {320 | 672 | 1056 | 1312 | 1584} BreakpointValue
* @event {{ size: BreakpointSize; breakpointValue: BreakpointValue; }} change * @event {{ size: BreakpointSize; breakpointValue: BreakpointValue; }} change
* @slot {{ size: BreakpointSize; sizes: Record<BreakpointSize, boolean>; }} * @slot {{ size: BreakpointSize; sizes: Record<BreakpointSize, boolean>; }}
*/ */

19
src/Breakpoint/breakpointObserver.d.ts vendored Normal file
View file

@ -0,0 +1,19 @@
/**
* Creates a readable store that returns the current {@link BreakpointSize}.
* It also provides functions for creating derived stores used to do comparisons.
*/
export function breakpointObserver(): {
subscribe: (this: void, run: import("svelte/store").Subscriber<any>, invalidate?: (value?: any) => void) => import("svelte/store").Unsubscriber;
/**
* Returns a store readable store that returns whether the current
* breakpoint is smaller than {@link size}.
* @param {BreakpointSize} size Size to compare against.
*/
smallerThan: (size: BreakpointSize) => import("svelte/store").Readable<boolean>;
/**
* Returns a store readable store that returns whether the current
* breakpoint is larger than {@link size}.
* @param {BreakpointSize} size Size to compare against.
*/
largerThan: (size: BreakpointSize) => import("svelte/store").Readable<boolean>;
};

View file

@ -53,19 +53,30 @@ export function breakpointObserver() {
return { return {
subscribe: store.subscribe, subscribe: store.subscribe,
/** /**
* Returns a store readable store that returns whether the current * Returns a store readable store that returns whether the current
* breakpoint smaller than {@link size}. * breakpoint is smaller than {@link size}.
* @param {BreakpointSize} size Size to compare against. * @param {BreakpointSize} size Size to compare against.
*/ */
smallerThan: (size) => smallerThan: (size) => {
derived(store, ($size) => breakpoints[$size] < breakpoints[size]), checkSizeValid(size);
return derived(store, ($size) => breakpoints[$size] < breakpoints[size]);
},
/** /**
* Returns a store readable store that returns whether the current * Returns a store readable store that returns whether the current
* breakpoint larger than {@link size}. * breakpoint is larger than {@link size}.
* @param {BreakpointSize} size Size to compare against. * @param {BreakpointSize} size Size to compare against.
*/ */
largerThan: (size) => largerThan: (size) => {
derived(store, ($size) => breakpoints[$size] > breakpoints[size]), checkSizeValid(size);
return derived(store, ($size) => breakpoints[$size] > breakpoints[size]);
},
}; };
} }
function checkSizeValid(size) {
if (size in breakpoints == false)
throw new Error(`"${size}" is not a valid breakpoint size.`);
}

5
src/Breakpoint/constants.d.ts vendored Normal file
View file

@ -0,0 +1,5 @@
/**
* Pixel sizes of Carbon grid breakpoints.
* @type {Record<BreakpointSize, BreakpointValue>}
*/
export const breakpoints: Record<BreakpointSize, BreakpointValue>;

2
src/Breakpoint/index.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export { breakpointObserver } from "./breakpointObserver";
export { breakpoints } from "./constants";

View file

@ -1,7 +1,7 @@
export { Accordion, AccordionItem, AccordionSkeleton } from "./Accordion"; export { Accordion, AccordionItem, AccordionSkeleton } from "./Accordion";
export { AspectRatio } from "./AspectRatio"; export { AspectRatio } from "./AspectRatio";
export { Breadcrumb, BreadcrumbItem, BreadcrumbSkeleton } from "./Breadcrumb"; export { Breadcrumb, BreadcrumbItem, BreadcrumbSkeleton } from "./Breadcrumb";
export { Breakpoint, breakpoints, breakpointObserver } from "./Breakpoint"; export { Breakpoint } from "./Breakpoint";
export { Button, ButtonSkeleton, ButtonSet } from "./Button"; export { Button, ButtonSkeleton, ButtonSet } from "./Button";
export { Checkbox, CheckboxSkeleton } from "./Checkbox"; export { Checkbox, CheckboxSkeleton } from "./Checkbox";
export { ContentSwitcher, Switch } from "./ContentSwitcher"; export { ContentSwitcher, Switch } from "./ContentSwitcher";

View file

@ -1,6 +1,10 @@
/// <reference types="svelte" /> /// <reference types="svelte" />
import { SvelteComponentTyped } from "svelte"; import { SvelteComponentTyped } from "svelte";
export type BreakpointSize = "sm" | "md" | "lg" | "xlg" | "max";
export type BreakpointValue = 320 | 672 | 1056 | 1312 | 1584;
export interface BreakpointProps { export interface BreakpointProps {
/** /**
* Determine the current Carbon grid breakpoint size * Determine the current Carbon grid breakpoint size

2
types/index.d.ts vendored
View file

@ -6,8 +6,6 @@ export { default as Breadcrumb } from "./Breadcrumb/Breadcrumb.svelte";
export { default as BreadcrumbItem } from "./Breadcrumb/BreadcrumbItem.svelte"; export { default as BreadcrumbItem } from "./Breadcrumb/BreadcrumbItem.svelte";
export { default as BreadcrumbSkeleton } from "./Breadcrumb/BreadcrumbSkeleton.svelte"; export { default as BreadcrumbSkeleton } from "./Breadcrumb/BreadcrumbSkeleton.svelte";
export { default as Breakpoint } from "./Breakpoint/Breakpoint.svelte"; export { default as Breakpoint } from "./Breakpoint/Breakpoint.svelte";
export { default as breakpoints } from "./Breakpoint";
export { default as breakpointObserver } from "./Breakpoint";
export { default as Button } from "./Button/Button.svelte"; export { default as Button } from "./Button/Button.svelte";
export { default as ButtonSkeleton } from "./Button/ButtonSkeleton.svelte"; export { default as ButtonSkeleton } from "./Button/ButtonSkeleton.svelte";
export { default as ButtonSet } from "./Button/ButtonSet.svelte"; export { default as ButtonSet } from "./Button/ButtonSet.svelte";