Skip to content

Commit

Permalink
use code fences to document props in readme
Browse files Browse the repository at this point in the history
improve readme test: now includes checking readme lists correct type and default value for primitive-type props
  • Loading branch information
janosh committed Sep 13, 2022
1 parent 36d9e77 commit e282a7f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 31 deletions.
96 changes: 82 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[![NPM version](https://img.shields.io/npm/v/svelte-bricks?color=blue&logo=NPM)](https://npmjs.com/package/svelte-bricks)
[![Netlify Status](https://api.netlify.com/api/v1/badges/c3213069-e3cc-45ef-a446-b2358b9a35fb/deploy-status)](https://app.netlify.com/sites/svelte-bricks/deploys)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/janosh/svelte-bricks/main.svg)](https://results.pre-commit.ci/latest/github/janosh/svelte-bricks/main)
[![Open in StackBlitz](https://img.shields.io/badge/Open%20in-StackBlitz-darkblue?logo=pytorchlightning)](https://stackblitz.com/github/janosh/svelte-bricks)
[![Open in StackBlitz](https://img.shields.io/badge/Open%20in-StackBlitz-darkblue?logo=stackblitz)](https://stackblitz.com/github/janosh/svelte-bricks)

</h4>

Expand Down Expand Up @@ -67,19 +67,87 @@ h)

Additional optional props are:

- `items: (string | number | object)[]`: required
- `minColWidth: number = 330` (in `px`)
- `maxColWidth: number = 500` (in `px`)
- `gap: number = 20` (in `px`)
- `masonryWidth: number = 0`: Bound to the masonry `div`s width (in `px`).
- `masonryHeight: number = 0`: Bound to the masonry `div`s height (in `px`).
- `idKey: string = 'id'`: Name of the attribute to use as identifier if items are objects.
- `getId: (item) => string | number`: Custom function that maps masonry items to unique IDs.
- `animate: boolean = true`: Whether to [FLIP-animate](https://svelte.dev/tutorial/animate) masonry items when viewport resizing or other events cause `items` to rearrange.
- `style: string = ''`: Inline styles that will be applied to the top-level `div.masonry`.
- `duration: number = 200`: Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.
- `class: string = ''`: Applies to the outer `div` wrapping all masonry columns. For use with CSS frameworks like Tailwind.
- `columnClass: string = ''`: Applies to each column `div`.
1. ```ts
animate: boolean = true
```

Whether to [FLIP-animate](https://svelte.dev/tutorial/animate) masonry items when viewport resizing or other events cause `items` to rearrange.

1. ```ts
class: string = ''
```

Applies to the outer `div` wrapping all masonry columns. For use with CSS frameworks like Tailwind.

1. ```ts
columnClass: string = ''
```

Applies to each column `div`.

1. ```ts
duration: number = 200
```

Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.

1. ```ts
gap: number = 20
```

Gap between columns and items within each column in `px`.

1. ```ts
getId: (Item) => string | number = (item: Item) => {
if (typeof item === `number`) return item
if (typeof item === `string`) return item
return (item as Record<string, unknown>)[idKey]
}
```

Custom function that maps masonry items to unique IDs of type `string` or `number`.

1. ```ts
idKey: string = 'id'
```

Name of the attribute to use as identifier if items are objects.

1. ```ts
items: (string | number | object)[]
```

required

1. ```ts
masonryHeight: number = 0
```

The masonry `div`s height in `px`.

1. ```ts
masonryWidth: number = 0
```

The masonry `div`s width in `px`.

1. ```ts
maxColWidth: number = 500
```

Maximum column width in `px`.

1. ```ts
minColWidth: number = 330
```

Minimum column width in `px`.

1. ```ts
style: string = ''
```

Inline styles that will be applied to the top-level `div.masonry`.

## Styling

Expand Down
5 changes: 4 additions & 1 deletion src/components/Slider.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script lang="ts">
export let value: number, label: string, min: number, max: number
export let value: number
export let label: string
export let min: number
export let max: number
</script>

<label>
Expand Down
21 changes: 10 additions & 11 deletions src/lib/Masonry.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@
import { flip } from 'svelte/animate'
import { fade } from 'svelte/transition'
export let items: Item[]
export let minColWidth = 330
export let maxColWidth = 500
export let gap = 20
export let masonryWidth = 0
export let masonryHeight = 0
export let animate = true
export let style = ``
export let duration = 200
export { className as class }
export let columnClass = ``
export let duration = 200
export let gap = 20
// On non-primitive types, we need a property to tell masonry items apart. This component
// hard-codes the name of this property to 'id'. See https://svelte.dev/tutorial/keyed-each-blocks.
export let idKey = `id`
export let getId = (item: Item) => {
if (typeof item === `string`) return item
if (typeof item === `number`) return item
if (typeof item === `string`) return item
return (item as Record<string, unknown>)[idKey]
}
export let idKey = `id`
export let items: Item[]
export let masonryHeight = 0
export let masonryWidth = 0
export let maxColWidth = 500
export let minColWidth = 330
export let style = ``
type Item = $$Generic
let className = ``
Expand Down
22 changes: 17 additions & 5 deletions tests/readme.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import Masonry from '$lib'
import { readFileSync } from 'fs'
import { expect, test } from 'vitest'
import Masonry from '../src/lib'

test(`readme documents all props`, () => {
const readme = readFileSync(`readme.md`, `utf8`)
const readme = readFileSync(`readme.md`, `utf8`)

test(`readme documents all props and their correct types and defaults`, () => {
const instance = new Masonry({
target: document.body,
props: { items: [] },
})
const { props, ctx } = instance.$$

for (const prop of Object.keys(instance.$$.props)) {
expect(readme).to.contain(`- \`${prop}: `)
for (const [prop, ctx_idx] of Object.entries(props)) {
let default_val = ctx[ctx_idx as number]
const type: string = typeof default_val

if (type === `string`) default_val = `'${default_val}'`

if ([`string`, `number`, `boolean`].includes(type)) {
const expected = `1. \`\`\`ts\n ${prop}: ${type} = ${default_val}\n \`\`\``

expect(readme).to.contain(expected)
} else {
expect(readme).to.contain(`1. \`\`\`ts\n ${prop}: `)
}
}
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"strict": true,
"module": "esnext",
Expand Down

0 comments on commit e282a7f

Please sign in to comment.