svelte-bricks 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,34 +12,36 @@
12
12
  }
13
13
  </style>
14
14
 
15
- <script >import { flip } from 'svelte/animate';
15
+ <script>import { flip } from 'svelte/animate';
16
16
  import { fade } from 'svelte/transition';
17
- export let items;
18
- export let minColWidth = 330;
19
- export let maxColWidth = 500;
20
- export let gap = 20;
21
- export let masonryWidth = 0;
22
- export let masonryHeight = 0;
23
17
  export let animate = true;
24
- export let style = ``;
25
- export let duration = 200;
26
18
  export { className as class };
27
19
  export let columnClass = ``;
20
+ export let duration = 200;
21
+ export let gap = 20;
22
+ // On non-primitive types, we need a property to tell masonry items apart. This component
23
+ // hard-codes the name of this property to 'id'. See https://svelte.dev/tutorial/keyed-each-blocks.
24
+ export let getId = (item) => {
25
+ if (typeof item === `number`)
26
+ return item;
27
+ if (typeof item === `string`)
28
+ return item;
29
+ return item[idKey];
30
+ };
31
+ export let idKey = `id`;
32
+ export let items;
33
+ export let masonryHeight = 0;
34
+ export let masonryWidth = 0;
35
+ export let maxColWidth = 500;
36
+ export let minColWidth = 330;
37
+ export let style = ``;
28
38
  let className = ``;
29
39
  $: nCols = Math.min(items.length, Math.floor(masonryWidth / (minColWidth + gap)) || 1);
30
40
  $: itemsToCols = items.reduce((cols, item, idx) => {
31
41
  cols[idx % cols.length].push([item, idx]);
32
42
  return cols;
33
- }, Array(nCols)
34
- .fill(null)
35
- .map(() => []));
36
- function getId(item) {
37
- if (typeof item === `string`)
38
- return item;
39
- if (typeof item === `number`)
40
- return item;
41
- return item.id;
42
- }
43
+ }, Array(nCols).fill(null).map(() => []) // prettier-ignore
44
+ );
43
45
  </script>
44
46
  <div
45
47
  class="masonry {className}"
@@ -50,18 +52,22 @@ function getId(item) {
50
52
  {#each itemsToCols as col}
51
53
  <div class="col {columnClass}" style="gap: {gap}px; max-width: {maxColWidth}px;">
52
54
  {#if animate}
53
- {#each col as [item, idx] (getId(item) || idx)}
55
+ {#each col as [item, idx] (getId(item))}
54
56
  <div
55
57
  in:fade|local={{ delay: 100, duration }}
56
58
  out:fade|local={{ delay: 0, duration }}
57
59
  animate:flip={{ duration }}
58
60
  >
59
- <slot {idx} {item} />
61
+ <slot {idx} {item}>
62
+ <span>{item}</span>
63
+ </slot>
60
64
  </div>
61
65
  {/each}
62
66
  {:else}
63
- {#each col as [item, idx] (getId(item) || idx)}
64
- <slot {idx} {item} />
67
+ {#each col as [item, idx] (getId(item))}
68
+ <slot {idx} {item}>
69
+ <span>{item}</span>
70
+ </slot>
65
71
  {/each}
66
72
  {/if}
67
73
  </div>
@@ -0,0 +1,33 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ declare class __sveltets_Render<Item> {
3
+ props(): {
4
+ animate?: boolean | undefined;
5
+ class?: string | undefined;
6
+ columnClass?: string | undefined;
7
+ duration?: number | undefined;
8
+ gap?: number | undefined;
9
+ getId?: ((item: Item) => string | number) | undefined;
10
+ idKey?: string | undefined;
11
+ items: Item[];
12
+ masonryHeight?: number | undefined;
13
+ masonryWidth?: number | undefined;
14
+ maxColWidth?: number | undefined;
15
+ minColWidth?: number | undefined;
16
+ style?: string | undefined;
17
+ };
18
+ events(): {} & {
19
+ [evt: string]: CustomEvent<any>;
20
+ };
21
+ slots(): {
22
+ default: {
23
+ idx: number;
24
+ item: Item;
25
+ };
26
+ };
27
+ }
28
+ export type MasonryProps<Item> = ReturnType<__sveltets_Render<Item>['props']>;
29
+ export type MasonryEvents<Item> = ReturnType<__sveltets_Render<Item>['events']>;
30
+ export type MasonrySlots<Item> = ReturnType<__sveltets_Render<Item>['slots']>;
31
+ export default class Masonry<Item> extends SvelteComponentTyped<MasonryProps<Item>, MasonryEvents<Item>, MasonrySlots<Item>> {
32
+ }
33
+ export {};
@@ -0,0 +1 @@
1
+ export { default, default as Masonry } from './Masonry.svelte';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default, default as Masonry } from './Masonry.svelte';
package/package.json CHANGED
@@ -2,32 +2,46 @@
2
2
  "name": "svelte-bricks",
3
3
  "description": "Simple masonry implementation without column balancing",
4
4
  "author": "Janosh Riebesell <janosh.riebesell@gmail.com>",
5
- "homepage": "https://svelte-bricks.netlify.app",
5
+ "homepage": "https://janosh.github.io/svelte-bricks",
6
6
  "repository": "https://github.com/janosh/svelte-bricks",
7
7
  "license": "MIT",
8
- "version": "0.1.6",
8
+ "version": "0.2.0",
9
9
  "type": "module",
10
- "svelte": "Masonry.svelte",
10
+ "svelte": "./dist/index.js",
11
11
  "bugs": "https://github.com/janosh/svelte-bricks/issues",
12
+ "scripts": {
13
+ "dev": "vite dev",
14
+ "build": "vite build",
15
+ "preview": "vite preview",
16
+ "package": "svelte-package",
17
+ "serve": "vite build && vite preview",
18
+ "check": "svelte-check --ignore dist",
19
+ "test": "vitest test",
20
+ "changelog": "npx auto-changelog --package --output changelog.md --hide-credit --commit-limit false"
21
+ },
22
+ "dependencies": {
23
+ "svelte": "^3.57.0"
24
+ },
12
25
  "devDependencies": {
13
- "@sveltejs/adapter-static": "^1.0.0-next.29",
14
- "@sveltejs/kit": "^1.0.0-next.310",
15
- "@typescript-eslint/eslint-plugin": "^5.18.0",
16
- "@typescript-eslint/parser": "^5.18.0",
17
- "eslint": "^8.13.0",
18
- "eslint-plugin-svelte3": "^3.4.1",
19
- "jsdom": "^19.0.0",
20
- "mdsvex": "^0.10.5",
21
- "prettier": "^2.6.2",
22
- "prettier-plugin-svelte": "^2.7.0",
23
- "svelte": "^3.47.0",
24
- "svelte-check": "^2.6.0",
25
- "svelte-github-corner": "^0.1.0",
26
- "svelte-preprocess": "^4.10.5",
27
- "svelte2tsx": "^0.5.8",
28
- "typescript": "^4.6.3",
29
- "vite": "^2.9.1",
30
- "vitest": "^0.9.3"
26
+ "@sveltejs/adapter-static": "^2.0.1",
27
+ "@sveltejs/kit": "^1.13.0",
28
+ "@sveltejs/package": "^2.0.2",
29
+ "@typescript-eslint/eslint-plugin": "^5.56.0",
30
+ "@typescript-eslint/parser": "^5.56.0",
31
+ "eslint": "^8.36.0",
32
+ "eslint-plugin-svelte3": "^4.0.0",
33
+ "jsdom": "^21.1.1",
34
+ "mdsvex": "^0.10.6",
35
+ "prettier": "^2.8.6",
36
+ "prettier-plugin-svelte": "^2.10.0",
37
+ "svelte-check": "^3.1.4",
38
+ "svelte-preprocess": "^5.0.3",
39
+ "svelte-toc": "^0.5.4",
40
+ "svelte-zoo": "^0.4.3",
41
+ "svelte2tsx": "^0.6.10",
42
+ "typescript": "^5.0.2",
43
+ "vite": "^4.2.1",
44
+ "vitest": "^0.29.7"
31
45
  },
32
46
  "keywords": [
33
47
  "svelte",
@@ -39,8 +53,18 @@
39
53
  "access": "public"
40
54
  },
41
55
  "exports": {
42
- "./package.json": "./package.json",
43
- "./Masonry.svelte": "./Masonry.svelte",
44
- ".": "./index.js"
45
- }
46
- }
56
+ "./Masonry.svelte": {
57
+ "types": "./dist/Masonry.svelte.d.ts",
58
+ "svelte": "./dist/Masonry.svelte",
59
+ "default": "./dist/Masonry.svelte"
60
+ },
61
+ ".": {
62
+ "types": "./dist/index.d.ts",
63
+ "svelte": "./dist/index.js",
64
+ "default": "./dist/index.js"
65
+ }
66
+ },
67
+ "files": [
68
+ "dist"
69
+ ]
70
+ }
package/readme.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <div class="hide-in-docs">
2
2
 
3
3
  <h1 align="center">
4
- <img src="https://raw.githubusercontent.com/janosh/svelte-bricks/main/static/favicon.svg" alt="Svelte Bricks" height=60>
4
+ <img src="https://raw.githubusercontent.com/janosh/svelte-bricks/main/static/favicon.svg" alt="Logo" height=60>
5
5
  <br>&ensp;Svelte Bricks
6
6
  </h1>
7
7
 
@@ -9,21 +9,20 @@
9
9
 
10
10
  [![Tests](https://github.com/janosh/svelte-bricks/actions/workflows/test.yml/badge.svg)](https://github.com/janosh/svelte-bricks/actions/workflows/test.yml)
11
11
  [![NPM version](https://img.shields.io/npm/v/svelte-bricks?color=blue&logo=NPM)](https://npmjs.com/package/svelte-bricks)
12
- [![Netlify Status](https://api.netlify.com/api/v1/badges/c3213069-e3cc-45ef-a446-b2358b9a35fb/deploy-status)](https://app.netlify.com/sites/svelte-bricks/deploys)
12
+ [![GitHub Pages](https://github.com/janosh/svelte-bricks/actions/workflows/gh-pages.yml/badge.svg)](https://github.com/janosh/svelte-bricks/actions/workflows/gh-pages.yml)
13
13
  [![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)
14
+ [![Open in StackBlitz](https://img.shields.io/badge/Open%20in-StackBlitz-darkblue?logo=stackblitz)](https://stackblitz.com/github/janosh/svelte-bricks)
14
15
 
15
16
  </h4>
16
17
 
17
- Naive implementation in Svelte without column balancing.
18
-
19
- **[Live demo](https://svelte-bricks.netlify.app)**
18
+ Naive implementation in Svelte without column balancing. **[Live demo](https://janosh.github.io/svelte-bricks)**
20
19
 
21
20
  </div>
22
21
 
23
22
  ## Installation
24
23
 
25
24
  ```sh
26
- yarn add -D svelte-bricks
25
+ npm install --dev svelte-bricks
27
26
  ```
28
27
 
29
28
  ## Usage
@@ -57,7 +56,7 @@ h)
57
56
  </Masonry>
58
57
  ```
59
58
 
60
- **Note**: On non-primitive types, i.e. if `items` is an array of objects, this component requires that each object have a key named `'id'` that contains a unique primitive value. This value is used to identify each item in the keyed `{#each}` block that renders the masonry layout. Without this, Svelte could not avoid duplicates when new items are added nor maintain order when existing ones are rearranged. Read the [Svelte docs](https://svelte.dev/tutorial/keyed-each-blocks) for details.
59
+ **Note**: If `items` is an array of objects, this component tries to access an `id` property on each item. This value is used to tell items apart in the keyed `{#each}` block that creates the masonry layout. Without it, Svelte could not avoid duplicates when new items are added or existing ones rearranged. Read the [Svelte docs](https://svelte.dev/tutorial/keyed-each-blocks) for details. To change the name of the identifier key, pass `idKey="some-uniq-key`. Or pass a function `getId = (item: Item) => string | number` that maps items to unique IDs.
61
60
 
62
61
  **Hint**: Balanced columns can be achieved even with this simple implementation if masonry items are allowed to stretch to the column height.
63
62
 
@@ -67,17 +66,87 @@ h)
67
66
 
68
67
  Additional optional props are:
69
68
 
70
- - `items: any[]`: required
71
- - `minColWidth: number = 330` (in `px`)
72
- - `maxColWidth: number = 500` (in `px`)
73
- - `gap: number = 20` (in `px`)
74
- - `masonryWidth: number = 0`: Bound to the masonry `div`s width (in `px`).
75
- - `masonryHeight: number = 0`: Bound to the masonry `div`s height (in `px`).
76
- - `animate: boolean = true`: Whether to [FLIP-animate](https://svelte.dev/tutorial/animate) masonry items when viewport resizing or other events cause `items` to rearrange.
77
- - `style: string = ''`: Inline styles that will be applied to the top-level `div.masonry`.
78
- - `duration: number = 200`: Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.
79
- - `class: string = ''`: Applies to the outer `div` wrapping all masonry columns. For use with CSS frameworks like Tailwind.
80
- - `columnClass: string = ''`: Applies to each column `div`.
69
+ 1. ```ts
70
+ animate: boolean = true
71
+ ```
72
+
73
+ Whether to [FLIP-animate](https://svelte.dev/tutorial/animate) masonry items when viewport resizing or other events cause `items` to rearrange.
74
+
75
+ 1. ```ts
76
+ class: string = ``
77
+ ```
78
+
79
+ Applies to the outer `div` wrapping all masonry columns. For use with CSS frameworks like Tailwind.
80
+
81
+ 1. ```ts
82
+ columnClass: string = ``
83
+ ```
84
+
85
+ Applies to each column `div`.
86
+
87
+ 1. ```ts
88
+ duration: number = 200
89
+ ```
90
+
91
+ Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.
92
+
93
+ 1. ```ts
94
+ gap: number = 20
95
+ ```
96
+
97
+ Gap between columns and items within each column in `px`.
98
+
99
+ 1. ```ts
100
+ getId = (item: Item): string | number => {
101
+ if (typeof item === `number`) return item
102
+ if (typeof item === `string`) return item
103
+ return item[idKey]
104
+ }
105
+ ```
106
+
107
+ Custom function that maps masonry items to unique IDs of type `string` or `number`.
108
+
109
+ 1. ```ts
110
+ idKey: string = `id`
111
+ ```
112
+
113
+ Name of the attribute to use as identifier if items are objects.
114
+
115
+ 1. ```ts
116
+ items: Item[]
117
+ ```
118
+
119
+ The only required prop are the list of items to render where `Item = $$Generic` is a generic type which usually will be `object` but can also be simple types `string` or `number`.
120
+
121
+ 1. ```ts
122
+ masonryHeight: number = 0
123
+ ```
124
+
125
+ The masonry `div`s height in `px`.
126
+
127
+ 1. ```ts
128
+ masonryWidth: number = 0
129
+ ```
130
+
131
+ The masonry `div`s width in `px`.
132
+
133
+ 1. ```ts
134
+ maxColWidth: number = 500
135
+ ```
136
+
137
+ Maximum column width in `px`.
138
+
139
+ 1. ```ts
140
+ minColWidth: number = 330
141
+ ```
142
+
143
+ Minimum column width in `px`.
144
+
145
+ 1. ```ts
146
+ style: string = ``
147
+ ```
148
+
149
+ Inline styles that will be applied to the top-level `div.masonry`.
81
150
 
82
151
  ## Styling
83
152
 
@@ -1,35 +0,0 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- items: (string | number | {
5
- id: string | number;
6
- })[];
7
- minColWidth?: number | undefined;
8
- maxColWidth?: number | undefined;
9
- gap?: number | undefined;
10
- masonryWidth?: number | undefined;
11
- masonryHeight?: number | undefined;
12
- animate?: boolean | undefined;
13
- style?: string | undefined;
14
- duration?: number | undefined;
15
- class?: string | undefined;
16
- columnClass?: string | undefined;
17
- };
18
- events: {
19
- [evt: string]: CustomEvent<any>;
20
- };
21
- slots: {
22
- default: {
23
- idx: number;
24
- item: string | number | {
25
- id: string | number;
26
- };
27
- };
28
- };
29
- };
30
- export declare type MasonryProps = typeof __propDef.props;
31
- export declare type MasonryEvents = typeof __propDef.events;
32
- export declare type MasonrySlots = typeof __propDef.slots;
33
- export default class Masonry extends SvelteComponentTyped<MasonryProps, MasonryEvents, MasonrySlots> {
34
- }
35
- export {};
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { default } from './Masonry.svelte';
package/index.js DELETED
@@ -1 +0,0 @@
1
- export { default } from './Masonry.svelte';