svelte-bricks 0.1.6 → 0.1.7

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.
package/Masonry.svelte CHANGED
@@ -12,7 +12,7 @@
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
17
  export let items;
18
18
  export let minColWidth = 330;
@@ -25,21 +25,23 @@ export let style = ``;
25
25
  export let duration = 200;
26
26
  export { className as class };
27
27
  export let columnClass = ``;
28
+ // On non-primitive types, we need a property to tell masonry items apart. This component
29
+ // hard-codes the name of this property to 'id'. See https://svelte.dev/tutorial/keyed-each-blocks.
30
+ export let idKey = `id`;
31
+ export let getId = (item) => {
32
+ if (typeof item === `string`)
33
+ return item;
34
+ if (typeof item === `number`)
35
+ return item;
36
+ return item[idKey];
37
+ };
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,7 +52,7 @@ 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 }}
@@ -60,7 +62,7 @@ function getId(item) {
60
62
  </div>
61
63
  {/each}
62
64
  {:else}
63
- {#each col as [item, idx] (getId(item) || idx)}
65
+ {#each col as [item, idx] (getId(item))}
64
66
  <slot {idx} {item} />
65
67
  {/each}
66
68
  {/if}
@@ -1,9 +1,7 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- items: (string | number | {
5
- id: string | number;
6
- })[];
2
+ declare class __sveltets_Render<Item> {
3
+ props(): {
4
+ items: Item[];
7
5
  minColWidth?: number | undefined;
8
6
  maxColWidth?: number | undefined;
9
7
  gap?: number | undefined;
@@ -14,22 +12,22 @@ declare const __propDef: {
14
12
  duration?: number | undefined;
15
13
  class?: string | undefined;
16
14
  columnClass?: string | undefined;
15
+ idKey?: string | undefined;
16
+ getId?: ((item: Item) => unknown) | undefined;
17
17
  };
18
- events: {
18
+ events(): {} & {
19
19
  [evt: string]: CustomEvent<any>;
20
20
  };
21
- slots: {
21
+ slots(): {
22
22
  default: {
23
23
  idx: number;
24
- item: string | number | {
25
- id: string | number;
26
- };
24
+ item: Item;
27
25
  };
28
26
  };
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> {
27
+ }
28
+ export declare type MasonryProps<Item> = ReturnType<__sveltets_Render<Item>['props']>;
29
+ export declare type MasonryEvents<Item> = ReturnType<__sveltets_Render<Item>['events']>;
30
+ export declare type MasonrySlots<Item> = ReturnType<__sveltets_Render<Item>['slots']>;
31
+ export default class Masonry<Item> extends SvelteComponentTyped<MasonryProps<Item>, MasonryEvents<Item>, MasonrySlots<Item>> {
34
32
  }
35
33
  export {};
package/package.json CHANGED
@@ -5,29 +5,29 @@
5
5
  "homepage": "https://svelte-bricks.netlify.app",
6
6
  "repository": "https://github.com/janosh/svelte-bricks",
7
7
  "license": "MIT",
8
- "version": "0.1.6",
8
+ "version": "0.1.7",
9
9
  "type": "module",
10
10
  "svelte": "Masonry.svelte",
11
11
  "bugs": "https://github.com/janosh/svelte-bricks/issues",
12
12
  "devDependencies": {
13
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",
14
+ "@sveltejs/kit": "^1.0.0-next.326",
15
+ "@typescript-eslint/eslint-plugin": "^5.23.0",
16
+ "@typescript-eslint/parser": "^5.23.0",
17
+ "eslint": "^8.15.0",
18
+ "eslint-plugin-svelte3": "^4.0.0",
19
19
  "jsdom": "^19.0.0",
20
20
  "mdsvex": "^0.10.5",
21
21
  "prettier": "^2.6.2",
22
22
  "prettier-plugin-svelte": "^2.7.0",
23
- "svelte": "^3.47.0",
24
- "svelte-check": "^2.6.0",
23
+ "svelte": "^3.48.0",
24
+ "svelte-check": "^2.7.0",
25
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
+ "svelte-preprocess": "^4.10.6",
27
+ "svelte2tsx": "^0.5.9",
28
+ "typescript": "^4.6.4",
29
+ "vite": "^2.9.8",
30
+ "vitest": "^0.12.3"
31
31
  },
32
32
  "keywords": [
33
33
  "svelte",
package/readme.md CHANGED
@@ -57,7 +57,7 @@ h)
57
57
  </Masonry>
58
58
  ```
59
59
 
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.
60
+ **Note**: On non-primitive items, i.e. if `items` is an array of objects, this component by default tries to access a key named `'id'` on each item. This value is used to tell items apart in the keyed `{#each}` block that renders 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, use the `idKey` prop. You can also pass in a custom function as `getId` that should map items to unique IDs.
61
61
 
62
62
  **Hint**: Balanced columns can be achieved even with this simple implementation if masonry items are allowed to stretch to the column height.
63
63
 
@@ -67,12 +67,14 @@ h)
67
67
 
68
68
  Additional optional props are:
69
69
 
70
- - `items: any[]`: required
70
+ - `items: (string | number | object)[]`: required
71
71
  - `minColWidth: number = 330` (in `px`)
72
72
  - `maxColWidth: number = 500` (in `px`)
73
73
  - `gap: number = 20` (in `px`)
74
74
  - `masonryWidth: number = 0`: Bound to the masonry `div`s width (in `px`).
75
75
  - `masonryHeight: number = 0`: Bound to the masonry `div`s height (in `px`).
76
+ - `idKey: string = 'id'`: Name of the attribute to use as identifier if items are objects.
77
+ - `getId: (item) => string | number`: Custom function that maps masonry items to unique IDs.
76
78
  - `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
79
  - `style: string = ''`: Inline styles that will be applied to the top-level `div.masonry`.
78
80
  - `duration: number = 200`: Transition duration in milli seconds when masonry items are rearranged or added/removed. Set to 0 to disable transitions.