svelte-bricks 0.2.0 → 0.3.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.
@@ -1,48 +1,26 @@
1
- <style>
2
- :where(div.masonry) {
3
- display: flex;
4
- justify-content: center;
5
- overflow-wrap: anywhere;
6
- box-sizing: border-box;
7
- }
8
- :where(div.masonry div.col) {
9
- display: grid;
10
- height: max-content;
11
- width: 100%;
12
- }
13
- </style>
14
-
15
- <script>import { flip } from 'svelte/animate';
1
+ <script lang="ts">import { flip } from 'svelte/animate';
16
2
  import { fade } from 'svelte/transition';
17
- export let animate = true;
18
- export { className as class };
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) => {
3
+ let { animate = true, calcCols = (masonryWidth, minColWidth, gap) => {
4
+ return Math.min(items.length, Math.floor((masonryWidth + gap) / (minColWidth + gap)) || 1);
5
+ }, columnClass = ``, duration = 200, gap = 20, getId = (item) => {
25
6
  if (typeof item === `number`)
26
7
  return item;
27
8
  if (typeof item === `string`)
28
9
  return item;
29
10
  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 = ``;
38
- let className = ``;
39
- $: nCols = Math.min(items.length, Math.floor(masonryWidth / (minColWidth + gap)) || 1);
40
- $: itemsToCols = items.reduce((cols, item, idx) => {
11
+ }, idKey = `id`, items, masonryHeight = $bindable(0), masonryWidth = $bindable(0), maxColWidth = 500, minColWidth = 330, style = ``, class: className = ``, children, } = $props();
12
+ $effect.pre(() => {
13
+ if (maxColWidth < minColWidth) {
14
+ console.warn(`svelte-bricks: maxColWidth (${maxColWidth}) < minColWidth (${minColWidth}).`);
15
+ }
16
+ });
17
+ let nCols = $derived(calcCols(masonryWidth, minColWidth, gap));
18
+ let itemsToCols = $derived(items.reduce((cols, item, idx) => {
41
19
  cols[idx % cols.length].push([item, idx]);
42
20
  return cols;
43
- }, Array(nCols).fill(null).map(() => []) // prettier-ignore
44
- );
21
+ }, Array(nCols).fill(null).map(() => [])));
45
22
  </script>
23
+
46
24
  <div
47
25
  class="masonry {className}"
48
26
  bind:clientWidth={masonryWidth}
@@ -54,23 +32,36 @@ $: itemsToCols = items.reduce((cols, item, idx) => {
54
32
  {#if animate}
55
33
  {#each col as [item, idx] (getId(item))}
56
34
  <div
57
- in:fade|local={{ delay: 100, duration }}
58
- out:fade|local={{ delay: 0, duration }}
35
+ in:fade={{ delay: 100, duration }}
36
+ out:fade={{ delay: 0, duration }}
59
37
  animate:flip={{ duration }}
60
38
  >
61
- <slot {idx} {item}>
39
+ {#if children}{@render children({ idx, item })}{:else}
62
40
  <span>{item}</span>
63
- </slot>
41
+ {/if}
64
42
  </div>
65
43
  {/each}
66
44
  {:else}
67
45
  {#each col as [item, idx] (getId(item))}
68
- <slot {idx} {item}>
46
+ {#if children}{@render children({ idx, item })}{:else}
69
47
  <span>{item}</span>
70
- </slot>
48
+ {/if}
71
49
  {/each}
72
50
  {/if}
73
51
  </div>
74
52
  {/each}
75
53
  </div>
76
54
 
55
+ <style>
56
+ :where(div.masonry) {
57
+ display: flex;
58
+ justify-content: center;
59
+ overflow-wrap: anywhere;
60
+ box-sizing: border-box;
61
+ }
62
+ :where(div.masonry div.col) {
63
+ display: grid;
64
+ height: max-content;
65
+ width: 100%;
66
+ }
67
+ </style>
@@ -1,33 +1,34 @@
1
- import { SvelteComponentTyped } from "svelte";
1
+ import type { Snippet } from 'svelte';
2
2
  declare class __sveltets_Render<Item> {
3
3
  props(): {
4
- animate?: boolean | undefined;
5
- class?: string | undefined;
6
- columnClass?: string | undefined;
7
- duration?: number | undefined;
8
- gap?: number | undefined;
4
+ animate?: boolean;
5
+ calcCols?: ((masonryWidth: number, minColWidth: number, gap: number) => number) | undefined;
6
+ columnClass?: string;
7
+ duration?: number;
8
+ gap?: number;
9
9
  getId?: ((item: Item) => string | number) | undefined;
10
- idKey?: string | undefined;
10
+ idKey?: string;
11
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
- };
12
+ masonryHeight?: number;
13
+ masonryWidth?: number;
14
+ maxColWidth?: number;
15
+ minColWidth?: number;
16
+ style?: string;
17
+ class?: string;
18
+ children?: Snippet;
26
19
  };
20
+ events(): {};
21
+ slots(): {};
22
+ bindings(): "masonryHeight" | "masonryWidth";
23
+ exports(): {};
27
24
  }
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>> {
25
+ interface $$IsomorphicComponent {
26
+ new <Item>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Item>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Item>['props']>, ReturnType<__sveltets_Render<Item>['events']>, ReturnType<__sveltets_Render<Item>['slots']>> & {
27
+ $$bindings?: ReturnType<__sveltets_Render<Item>['bindings']>;
28
+ } & ReturnType<__sveltets_Render<Item>['exports']>;
29
+ <Item>(internal: unknown, props: ReturnType<__sveltets_Render<Item>['props']> & {}): ReturnType<__sveltets_Render<Item>['exports']>;
30
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
32
31
  }
33
- export {};
32
+ declare const Masonry: $$IsomorphicComponent;
33
+ type Masonry<Item> = InstanceType<typeof Masonry<Item>>;
34
+ export default Masonry;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "homepage": "https://janosh.github.io/svelte-bricks",
6
6
  "repository": "https://github.com/janosh/svelte-bricks",
7
7
  "license": "MIT",
8
- "version": "0.2.0",
8
+ "version": "0.3.0",
9
9
  "type": "module",
10
10
  "svelte": "./dist/index.js",
11
11
  "bugs": "https://github.com/janosh/svelte-bricks/issues",
@@ -20,28 +20,30 @@
20
20
  "changelog": "npx auto-changelog --package --output changelog.md --hide-credit --commit-limit false"
21
21
  },
22
22
  "dependencies": {
23
- "svelte": "^3.57.0"
23
+ "svelte": "^5.20.1"
24
24
  },
25
25
  "devDependencies": {
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"
26
+ "@stylistic/eslint-plugin": "^3.1.0",
27
+ "@sveltejs/adapter-static": "^3.0.8",
28
+ "@sveltejs/kit": "^2.17.2",
29
+ "@sveltejs/package": "^2.3.10",
30
+ "@typescript-eslint/eslint-plugin": "^8.24.0",
31
+ "@typescript-eslint/parser": "^8.24.0",
32
+ "eslint": "^9.20.1",
33
+ "eslint-plugin-svelte": "^2.46.1",
34
+ "jsdom": "^26.0.0",
35
+ "mdsvex": "^0.12.3",
36
+ "prettier": "^3.5.1",
37
+ "prettier-plugin-svelte": "^3.3.3",
38
+ "svelte-check": "^4.1.4",
39
+ "svelte-preprocess": "^6.0.3",
40
+ "svelte-toc": "^0.5.9",
41
+ "svelte-zoo": "^0.4.16",
42
+ "svelte2tsx": "^0.7.34",
43
+ "typescript": "^5.7.3",
44
+ "typescript-eslint": "^8.24.0",
45
+ "vite": "^6.1.0",
46
+ "vitest": "^3.0.5"
45
47
  },
46
48
  "keywords": [
47
49
  "svelte",
@@ -64,6 +66,7 @@
64
66
  "default": "./dist/index.js"
65
67
  }
66
68
  },
69
+ "types": "./dist/index.d.ts",
67
70
  "files": [
68
71
  "dist"
69
72
  ]
package/readme.md CHANGED
@@ -49,8 +49,8 @@ h)
49
49
  {maxColWidth}
50
50
  {gap}
51
51
  let:item
52
- bind:width
53
- bind:height
52
+ bind:masonryWidth={width}
53
+ bind:masonryHeight={height}
54
54
  >
55
55
  <Some {item} />
56
56
  </Masonry>
@@ -72,6 +72,21 @@ Additional optional props are:
72
72
 
73
73
  Whether to [FLIP-animate](https://svelte.dev/tutorial/animate) masonry items when viewport resizing or other events cause `items` to rearrange.
74
74
 
75
+ 1. ```ts
76
+ calcCols = (
77
+ masonryWidth: number,
78
+ minColWidth: number,
79
+ gap: number,
80
+ ): number => {
81
+ return Math.min(
82
+ items.length,
83
+ Math.floor((masonryWidth + gap) / (minColWidth + gap)) || 1,
84
+ )
85
+ }
86
+ ```
87
+
88
+ Function used to compute the number of columns based on the masonry width, minimum column width and gap.
89
+
75
90
  1. ```ts
76
91
  class: string = ``
77
92
  ```