svelte-bricks 0.2.1 → 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,25 @@
1
- <script>import { flip } from 'svelte/animate';
1
+ <script lang="ts">import { flip } from 'svelte/animate';
2
2
  import { fade } from 'svelte/transition';
3
- export let animate = true;
4
- export { className as class };
5
- export let columnClass = ``;
6
- export let duration = 200;
7
- export let gap = 20;
8
- // On non-primitive types, we need a property to tell masonry items apart. This component
9
- // hard-codes the name of this property to 'id'. See https://svelte.dev/tutorial/keyed-each-blocks.
10
- 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) => {
11
6
  if (typeof item === `number`)
12
7
  return item;
13
8
  if (typeof item === `string`)
14
9
  return item;
15
10
  return item[idKey];
16
- };
17
- export let idKey = `id`;
18
- export let items;
19
- export let masonryHeight = 0;
20
- export let masonryWidth = 0;
21
- export let maxColWidth = 500;
22
- export let minColWidth = 330;
23
- export let style = ``;
24
- let className = ``;
25
- $: nCols = Math.min(items.length, Math.floor(masonryWidth / (minColWidth + gap)) || 1);
26
- $: 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) => {
27
19
  cols[idx % cols.length].push([item, idx]);
28
20
  return cols;
29
- }, Array(nCols).fill(null).map(() => []) // prettier-ignore
30
- );
21
+ }, Array(nCols).fill(null).map(() => [])));
31
22
  </script>
32
- <style>
33
- :where(div.masonry) {
34
- display: flex;
35
- justify-content: center;
36
- overflow-wrap: anywhere;
37
- box-sizing: border-box;
38
- }
39
- :where(div.masonry div.col) {
40
- display: grid;
41
- height: max-content;
42
- width: 100%;
43
- }
44
- </style>
45
-
46
23
 
47
24
  <div
48
25
  class="masonry {className}"
@@ -59,19 +36,32 @@ $: itemsToCols = items.reduce((cols, item, idx) => {
59
36
  out:fade={{ delay: 0, duration }}
60
37
  animate:flip={{ duration }}
61
38
  >
62
- <slot {idx} {item}>
39
+ {#if children}{@render children({ idx, item })}{:else}
63
40
  <span>{item}</span>
64
- </slot>
41
+ {/if}
65
42
  </div>
66
43
  {/each}
67
44
  {:else}
68
45
  {#each col as [item, idx] (getId(item))}
69
- <slot {idx} {item}>
46
+ {#if children}{@render children({ idx, item })}{:else}
70
47
  <span>{item}</span>
71
- </slot>
48
+ {/if}
72
49
  {/each}
73
50
  {/if}
74
51
  </div>
75
52
  {/each}
76
53
  </div>
77
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 { SvelteComponent } 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 SvelteComponent<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.1",
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": "^4.2.1"
23
+ "svelte": "^5.20.1"
24
24
  },
25
25
  "devDependencies": {
26
- "@sveltejs/adapter-static": "^2.0.3",
27
- "@sveltejs/kit": "^1.25.0",
28
- "@sveltejs/package": "^2.2.2",
29
- "@typescript-eslint/eslint-plugin": "^6.7.2",
30
- "@typescript-eslint/parser": "^6.7.2",
31
- "eslint": "^8.49.0",
32
- "eslint-plugin-svelte": "^2.33.2",
33
- "jsdom": "^22.1.0",
34
- "mdsvex": "^0.11.0",
35
- "prettier": "^3.0.3",
36
- "prettier-plugin-svelte": "^3.0.3",
37
- "svelte-check": "^3.5.2",
38
- "svelte-preprocess": "^5.0.4",
39
- "svelte-toc": "^0.5.6",
40
- "svelte-zoo": "^0.4.9",
41
- "svelte2tsx": "^0.6.22",
42
- "typescript": "^5.2.2",
43
- "vite": "^4.4.9",
44
- "vitest": "^0.34.5"
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",
package/readme.md CHANGED
@@ -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
  ```