@masonry-grid/nanoviews 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/effect.d.ts +26 -0
- package/dist/effect.d.ts.map +1 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +93 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +45 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 - present, TrigenSoftware
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Nanoviews Masonry Grid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<img align="right" width="150" height="150" alt="Logo" src="../../website/src/assets/logo.svg">
|
|
4
|
+
|
|
5
|
+
[![ESM-only package][package]][package-url]
|
|
6
|
+
[![NPM version][npm]][npm-url]
|
|
7
|
+
[![Install size][size]][size-url]
|
|
8
|
+
[![Build status][build]][build-url]
|
|
9
|
+
[![Coverage status][coverage]][coverage-url]
|
|
10
|
+
|
|
11
|
+
[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg
|
|
12
|
+
[package-url]: https://nodejs.org/api/esm.html
|
|
13
|
+
|
|
14
|
+
[npm]: https://img.shields.io/npm/v/%40masonry-grid%2Fnanoviews.svg
|
|
15
|
+
[npm-url]: https://npmjs.com/package/@masonry-grid/nanoviews
|
|
16
|
+
|
|
17
|
+
[size]: https://deno.bundlejs.com/badge?q=%40masonry-grid%2Fnanoviews
|
|
18
|
+
[size-url]: https://bundlejs.com/?q=%40masonry-grid%2Fnanoviews
|
|
19
|
+
|
|
20
|
+
[build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/masonry-grid/tests.yml?branch=main
|
|
21
|
+
[build-url]: https://github.com/TrigenSoftware/masonry-grid/actions
|
|
22
|
+
|
|
23
|
+
[coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/masonry-grid.svg
|
|
24
|
+
[coverage-url]: https://app.codecov.io/gh/TrigenSoftware/masonry-grid
|
|
25
|
+
|
|
26
|
+
A fast, lightweight, and responsive masonry grid layout library for [Nanoviews](https://nanoviews.js.org).
|
|
27
|
+
|
|
28
|
+
- 🪶 **Lightweight**. ~1.7 kB (minified and brotlied). Zero dependencies except Nanoviews.
|
|
29
|
+
- ⚡ **Fast**. Built on top of [@masonry-grid/vanilla](../vanilla) with optimized reflow algorithms.
|
|
30
|
+
- 📱 **Responsive**. Automatically adapts to container size changes using [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
|
|
31
|
+
- 📡 **Reactive**. Every prop takes a plain value or a signal.
|
|
32
|
+
- 📘 **TypeScript**-first.
|
|
33
|
+
|
|
34
|
+
_Read the docs and explore examples at [masonry-grid.js.org](https://masonry-grid.js.org)_
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { img, component$ } from 'nanoviews'
|
|
38
|
+
import { BalancedMasonryGrid as MasonryGrid, Frame } from '@masonry-grid/nanoviews'
|
|
39
|
+
|
|
40
|
+
const Gallery = component$(() => (
|
|
41
|
+
MasonryGrid({
|
|
42
|
+
frameWidth: 200,
|
|
43
|
+
gap: 10
|
|
44
|
+
})(
|
|
45
|
+
Frame({
|
|
46
|
+
width: 4,
|
|
47
|
+
height: 3
|
|
48
|
+
})(
|
|
49
|
+
img({
|
|
50
|
+
src: 'https://picsum.photos/400/300',
|
|
51
|
+
alt: 'Photo 1'
|
|
52
|
+
})
|
|
53
|
+
),
|
|
54
|
+
Frame({
|
|
55
|
+
width: 1,
|
|
56
|
+
height: 1
|
|
57
|
+
})(
|
|
58
|
+
img({
|
|
59
|
+
src: 'https://picsum.photos/200/200',
|
|
60
|
+
alt: 'Photo 2'
|
|
61
|
+
})
|
|
62
|
+
),
|
|
63
|
+
Frame({
|
|
64
|
+
width: 3,
|
|
65
|
+
height: 4
|
|
66
|
+
})(
|
|
67
|
+
img({
|
|
68
|
+
src: 'https://picsum.photos/300/400',
|
|
69
|
+
alt: 'Photo 3'
|
|
70
|
+
})
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Install
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pnpm add @masonry-grid/nanoviews
|
|
80
|
+
# or
|
|
81
|
+
npm i @masonry-grid/nanoviews
|
|
82
|
+
# or
|
|
83
|
+
yarn add @masonry-grid/nanoviews
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Docs
|
|
87
|
+
|
|
88
|
+
- [Guides](https://masonry-grid.js.org/guides/prerequisites/)
|
|
89
|
+
- [API Reference](https://masonry-grid.js.org/api/nanoviews/)
|
|
90
|
+
- [Examples](https://masonry-grid.js.org/examples/#nanoviews)
|
package/dist/effect.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BaseMasonryGrid } from '@masonry-grid/vanilla';
|
|
2
|
+
import { type Signalish, type WritableSignal } from 'nanoviews/store';
|
|
3
|
+
export interface MasonryGridConstructor {
|
|
4
|
+
prototype: BaseMasonryGrid;
|
|
5
|
+
new (container: HTMLElement): BaseMasonryGrid;
|
|
6
|
+
}
|
|
7
|
+
export interface MasonryGridHookProps {
|
|
8
|
+
/**
|
|
9
|
+
* The MasonryGrid constructor to use.
|
|
10
|
+
*/
|
|
11
|
+
type: MasonryGridConstructor;
|
|
12
|
+
/**
|
|
13
|
+
* If true, the MasonryGrid will not be initialized.
|
|
14
|
+
*/
|
|
15
|
+
disabled?: Signalish<boolean | undefined>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Hook to create a MasonryGrid on the element that the returned signal receives as a `ref`.
|
|
19
|
+
* Call it while a view is built, in the render of a component.
|
|
20
|
+
* @param params
|
|
21
|
+
* @param params.type - The MasonryGrid constructor to use.
|
|
22
|
+
* @param params.disabled - If true, the MasonryGrid will not be initialized.
|
|
23
|
+
* @returns A signal to pass to the `ref` of the container element.
|
|
24
|
+
*/
|
|
25
|
+
export declare function useMasonryGrid({ type: MasonryGrid, disabled }: MasonryGridHookProps): WritableSignal<HTMLElement | null>;
|
|
26
|
+
//# sourceMappingURL=effect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effect.d.ts","sourceRoot":"","sources":["../src/effect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAC5D,OAAO,EACL,KAAK,SAAS,EACd,KAAK,cAAc,EAGpB,MAAM,iBAAiB,CAAA;AAGxB,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,eAAe,CAAA;IAC1B,KAAK,SAAS,EAAE,WAAW,GAAG,eAAe,CAAA;CAC9C;AAED,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,IAAI,EAAE,sBAAsB,CAAA;IAC5B;;OAEG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC,OAAO,GAAG,SAAS,CAAC,CAAA;CAC1C;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,EAC7B,IAAI,EAAE,WAAW,EACjB,QAAQ,EACT,EAAE,oBAAoB,GAAG,cAAc,CAAC,WAAW,GAAG,IAAI,CAAC,CAgB3D"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Signalish } from 'nanoviews/store';
|
|
2
|
+
import { type PolymorphicProps, type StyleValue } from 'nanoviews';
|
|
3
|
+
export * from './effect.js';
|
|
4
|
+
export interface MasonryGridProps extends PolymorphicProps<'div'> {
|
|
5
|
+
/**
|
|
6
|
+
* Minimum width of each frame of the grid.
|
|
7
|
+
* If not provided, the grid will auto-fit as many columns as possible.
|
|
8
|
+
*/
|
|
9
|
+
frameWidth?: Signalish<number | string | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* Grid gap between items.
|
|
12
|
+
*/
|
|
13
|
+
gap?: Signalish<number | string | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* If true, the MasonryGrid will not be initialized.
|
|
16
|
+
*/
|
|
17
|
+
disabled?: Signalish<boolean | undefined>;
|
|
18
|
+
}
|
|
19
|
+
export declare const RegularMasonryGrid: import("nanoviews").PolymorphicComponent<import("nanoviews").ElementFactory<"div">, import("nanoviews").PolymorphicOwnProps<MasonryGridProps>, import("nanoviews").Children, import("nanoviews").ElementFactory<"div"> | import("nanoviews").PolymorphicTarget>;
|
|
20
|
+
export declare const BalancedMasonryGrid: import("nanoviews").PolymorphicComponent<import("nanoviews").ElementFactory<"div">, import("nanoviews").PolymorphicOwnProps<MasonryGridProps>, import("nanoviews").Children, import("nanoviews").ElementFactory<"div"> | import("nanoviews").PolymorphicTarget>;
|
|
21
|
+
export interface FrameProps extends PolymorphicProps<'div'> {
|
|
22
|
+
/**
|
|
23
|
+
* Width of the frame.
|
|
24
|
+
* Not necessarily real width, but used for aspect ratio calculation.
|
|
25
|
+
*/
|
|
26
|
+
width: Signalish<number>;
|
|
27
|
+
/**
|
|
28
|
+
* Height of the frame.
|
|
29
|
+
* Not necessarily real height, but used for aspect ratio calculation.
|
|
30
|
+
*/
|
|
31
|
+
height: Signalish<number>;
|
|
32
|
+
}
|
|
33
|
+
export declare const Frame: import("nanoviews").PolymorphicComponent<import("nanoviews").ElementFactory<"div">, import("nanoviews").PolymorphicOwnProps<FrameProps>, import("nanoviews").Children, import("nanoviews").ElementFactory<"div"> | import("nanoviews").PolymorphicTarget>;
|
|
34
|
+
export interface SpannedMasonryGridProps extends PolymorphicProps<'div'> {
|
|
35
|
+
/**
|
|
36
|
+
* Style for the inner container element.
|
|
37
|
+
*/
|
|
38
|
+
innerStyle?: StyleValue;
|
|
39
|
+
/**
|
|
40
|
+
* Minimum width of each frame of the grid.
|
|
41
|
+
* If not provided, the grid will auto-fit as many columns as possible.
|
|
42
|
+
*/
|
|
43
|
+
frameWidth?: Signalish<number | string | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Grid gap between items.
|
|
46
|
+
*/
|
|
47
|
+
gap?: Signalish<number | string | undefined>;
|
|
48
|
+
/**
|
|
49
|
+
* Precision for span calculation.
|
|
50
|
+
* Affects how accurately frames will maintain their aspect ratios.
|
|
51
|
+
* Higher precision results in more accurate sizes but may impact performance and cause bugs in some browsers.
|
|
52
|
+
*/
|
|
53
|
+
precision?: Signalish<number | undefined>;
|
|
54
|
+
}
|
|
55
|
+
export declare const SpannedMasonryGrid: import("nanoviews").PolymorphicComponent<import("nanoviews").ElementFactory<"div">, import("nanoviews").PolymorphicOwnProps<SpannedMasonryGridProps>, import("nanoviews").Children, import("nanoviews").ElementFactory<"div"> | import("nanoviews").PolymorphicTarget>;
|
|
56
|
+
export interface SpannedFrameProps extends FrameProps {
|
|
57
|
+
/**
|
|
58
|
+
* Style for the inner container element.
|
|
59
|
+
*/
|
|
60
|
+
innerStyle?: StyleValue;
|
|
61
|
+
}
|
|
62
|
+
export declare const SpannedFrame: import("nanoviews").PolymorphicComponent<import("nanoviews").ElementFactory<"div">, import("nanoviews").PolymorphicOwnProps<SpannedFrameProps>, import("nanoviews").Children, import("nanoviews").ElementFactory<"div"> | import("nanoviews").PolymorphicTarget>;
|
|
63
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EAEf,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAGhB,MAAM,WAAW,CAAA;AAWlB,cAAc,aAAa,CAAA;AAgB3B,MAAM,WAAW,gBAAiB,SAAQ,gBAAgB,CAAC,KAAK,CAAC;IAC/D;;;OAGG;IACH,UAAU,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;IACnD;;OAEG;IACH,GAAG,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;IAC5C;;OAEG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC,OAAO,GAAG,SAAS,CAAC,CAAA;CAC1C;AAqCD,eAAO,MAAM,kBAAkB,iQAA6D,CAAA;AAE5F,eAAO,MAAM,mBAAmB,iQAA8D,CAAA;AAE9F,MAAM,WAAW,UAAW,SAAQ,gBAAgB,CAAC,KAAK,CAAC;IACzD;;;OAGG;IACH,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAA;IACxB;;;OAGG;IACH,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,CAAA;CAC1B;AAED,eAAO,MAAM,KAAK,2PAqBhB,CAAA;AAEF,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB,CAAC,KAAK,CAAC;IACtE;;OAEG;IACH,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB;;;OAGG;IACH,UAAU,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;IACnD;;OAEG;IACH,GAAG,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;IAC5C;;;;OAIG;IACH,SAAS,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;CAC1C;AAUD,eAAO,MAAM,kBAAkB,wQA8B7B,CAAA;AAEF,MAAM,WAAW,iBAAkB,SAAQ,UAAU;IACnD;;OAEG;IACH,UAAU,CAAC,EAAE,UAAU,CAAA;CACxB;AAcD,eAAO,MAAM,YAAY,kQA6BvB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { $get as e, signal as t } from "nanoviews/store";
|
|
2
|
+
import { div as n, effect$ as r, polymorphic$ as i } from "nanoviews";
|
|
3
|
+
import { BalancedMasonryGrid as a, RegularMasonryGrid as o } from "@masonry-grid/vanilla";
|
|
4
|
+
// src/effect.ts
|
|
5
|
+
function s({ type: n, disabled: i }) {
|
|
6
|
+
let a = t(null);
|
|
7
|
+
return r(() => {
|
|
8
|
+
let t = a();
|
|
9
|
+
if (!t || e(i)) return;
|
|
10
|
+
let r = new n(t);
|
|
11
|
+
return () => r.destroy();
|
|
12
|
+
}), a;
|
|
13
|
+
}
|
|
14
|
+
// src/utils.ts
|
|
15
|
+
function c(e) {
|
|
16
|
+
return e === void 0 ? e : typeof e == "number" ? `${e}px` : e;
|
|
17
|
+
}
|
|
18
|
+
// src/index.ts
|
|
19
|
+
function l(e, t) {
|
|
20
|
+
return {
|
|
21
|
+
display: "grid",
|
|
22
|
+
overflow: "hidden",
|
|
23
|
+
gridTemplateColumns: e === void 0 ? void 0 : `repeat(auto-fill, minmax(${c(e)}, 1fr))`,
|
|
24
|
+
gap: c(t)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
28
|
+
function u(t) {
|
|
29
|
+
return i(({ as: r = n, ref: i, frameWidth: a, gap: o, disabled: c, style: u, ...d }, f) => {
|
|
30
|
+
let p = s({
|
|
31
|
+
type: t,
|
|
32
|
+
disabled: c
|
|
33
|
+
});
|
|
34
|
+
return r({
|
|
35
|
+
...d,
|
|
36
|
+
ref(e) {
|
|
37
|
+
p(e), i?.(e);
|
|
38
|
+
},
|
|
39
|
+
style: () => ({
|
|
40
|
+
...l(e(a), e(o)),
|
|
41
|
+
...e(u)
|
|
42
|
+
})
|
|
43
|
+
})(...f);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
var d = /* @__PURE__ */ u(o), f = /* @__PURE__ */ u(a), p = /* @__PURE__ */ i(({ as: t = n, width: r, height: i, style: a, ...o }, s) => t({
|
|
47
|
+
...o,
|
|
48
|
+
style: () => ({
|
|
49
|
+
"--width": e(r),
|
|
50
|
+
"--height": e(i),
|
|
51
|
+
aspectRatio: "var(--width) / var(--height)",
|
|
52
|
+
...e(a)
|
|
53
|
+
})
|
|
54
|
+
})(...s)), m = 10, h = {
|
|
55
|
+
display: "grid",
|
|
56
|
+
clipPath: "margin-box",
|
|
57
|
+
margin: "calc(-1 * var(--gap, 0px) / 2)",
|
|
58
|
+
gridTemplateColumns: "repeat(auto-fill, minmax(var(--frame-width), 1fr))"
|
|
59
|
+
}, g = /* @__PURE__ */ i(({ as: t = n, precision: r, frameWidth: i, gap: a, style: o, innerStyle: s, ...l }, u) => t({
|
|
60
|
+
...l,
|
|
61
|
+
style: () => ({
|
|
62
|
+
"--frame-width": c(e(i)),
|
|
63
|
+
"--gap": c(e(a)),
|
|
64
|
+
"--precision": e(r) ?? m,
|
|
65
|
+
...e(o)
|
|
66
|
+
})
|
|
67
|
+
})(n({ style: () => ({
|
|
68
|
+
...h,
|
|
69
|
+
...e(s)
|
|
70
|
+
}) })(...u))), _ = {
|
|
71
|
+
aspectRatio: "var(--width) / var(--height)",
|
|
72
|
+
width: "100%",
|
|
73
|
+
height: "100%",
|
|
74
|
+
position: "relative",
|
|
75
|
+
gridRow: "span calc(var(--height) / var(--width) * var(--precision))"
|
|
76
|
+
}, v = {
|
|
77
|
+
position: "absolute",
|
|
78
|
+
inset: "calc(var(--gap, 0px) / 2)"
|
|
79
|
+
}, y = /* @__PURE__ */ i(({ as: t = n, width: r, height: i, style: a, innerStyle: o, ...s }, c) => t({
|
|
80
|
+
...s,
|
|
81
|
+
style: () => ({
|
|
82
|
+
"--width": e(r),
|
|
83
|
+
"--height": e(i),
|
|
84
|
+
..._,
|
|
85
|
+
...e(a)
|
|
86
|
+
})
|
|
87
|
+
})(n({ style: () => ({
|
|
88
|
+
...v,
|
|
89
|
+
...e(o)
|
|
90
|
+
}) })(...c)));
|
|
91
|
+
export { f as BalancedMasonryGrid, p as Frame, d as RegularMasonryGrid, y as SpannedFrame, g as SpannedMasonryGrid, s as useMasonryGrid };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["VanillaRegularMasonryGrid","VanillaBalancedMasonryGrid","$get","signal","div","effect$","polymorphic$","BalancedMasonryGrid$1","RegularMasonryGrid$1","useMasonryGrid","MasonryGrid","disabled","$container","container","masonryGrid","formatUnit","value","getContainerStyle","frameWidth","gap","createComponent","type","as","ref","style","props","children","element","RegularMasonryGrid","BalancedMasonryGrid","Frame","width","height","DEFAULT_SPAN_PRECISION","SPANNED_INNER_STYLE","SpannedMasonryGrid","precision","innerStyle","SPANNED_FRAME_STYLE","SPANNED_FRAME_INNER_STYLE","SpannedFrame"],"sources":["../src/effect.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { BaseMasonryGrid } from '@masonry-grid/vanilla'\nimport {\n type Signalish,\n type WritableSignal,\n $get,\n signal\n} from 'nanoviews/store'\nimport { effect$ } from 'nanoviews'\n\nexport interface MasonryGridConstructor {\n prototype: BaseMasonryGrid\n new (container: HTMLElement): BaseMasonryGrid\n}\n\nexport interface MasonryGridHookProps {\n /**\n * The MasonryGrid constructor to use.\n */\n type: MasonryGridConstructor\n /**\n * If true, the MasonryGrid will not be initialized.\n */\n disabled?: Signalish<boolean | undefined>\n}\n\n/**\n * Hook to create a MasonryGrid on the element that the returned signal receives as a `ref`.\n * Call it while a view is built, in the render of a component.\n * @param params\n * @param params.type - The MasonryGrid constructor to use.\n * @param params.disabled - If true, the MasonryGrid will not be initialized.\n * @returns A signal to pass to the `ref` of the container element.\n */\nexport function useMasonryGrid({\n type: MasonryGrid,\n disabled\n}: MasonryGridHookProps): WritableSignal<HTMLElement | null> {\n const $container = signal<HTMLElement | null>(null)\n\n effect$(() => {\n const container = $container()\n\n if (!container || $get(disabled)) {\n return\n }\n\n const masonryGrid = new MasonryGrid(container)\n\n return () => masonryGrid.destroy()\n })\n\n return $container\n}\n","export function formatUnit(value: string | number | undefined) {\n return value === undefined\n ? value\n : typeof value === 'number' ? `${value}px` : value\n}\n","import {\n type Signalish,\n $get\n} from 'nanoviews/store'\nimport {\n type CSSProperties,\n type PolymorphicProps,\n type StyleValue,\n div,\n polymorphic$\n} from 'nanoviews'\nimport {\n RegularMasonryGrid as VanillaRegularMasonryGrid,\n BalancedMasonryGrid as VanillaBalancedMasonryGrid\n} from '@masonry-grid/vanilla'\nimport {\n type MasonryGridConstructor,\n useMasonryGrid\n} from './effect.js'\nimport { formatUnit } from './utils.js'\n\nexport * from './effect.js'\n\nfunction getContainerStyle(\n frameWidth: number | string | undefined,\n gap: number | string | undefined\n): CSSProperties {\n return {\n display: 'grid',\n overflow: 'hidden',\n gridTemplateColumns: frameWidth === undefined\n ? undefined\n : `repeat(auto-fill, minmax(${formatUnit(frameWidth)}, 1fr))`,\n gap: formatUnit(gap)\n }\n}\n\nexport interface MasonryGridProps extends PolymorphicProps<'div'> {\n /**\n * Minimum width of each frame of the grid.\n * If not provided, the grid will auto-fit as many columns as possible.\n */\n frameWidth?: Signalish<number | string | undefined>\n /**\n * Grid gap between items.\n */\n gap?: Signalish<number | string | undefined>\n /**\n * If true, the MasonryGrid will not be initialized.\n */\n disabled?: Signalish<boolean | undefined>\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nfunction createComponent(type: MasonryGridConstructor) {\n return polymorphic$((\n {\n as = div,\n ref,\n frameWidth,\n gap,\n disabled,\n style,\n ...props\n }: MasonryGridProps,\n children\n ) => {\n const $container = useMasonryGrid({\n type,\n disabled\n })\n\n return as({\n ...props,\n ref(element) {\n $container(element)\n ref?.(element)\n },\n style: () => ({\n ...getContainerStyle($get(frameWidth), $get(gap)),\n ...$get(style)\n })\n })(\n ...children\n )\n })\n}\n\nexport const RegularMasonryGrid = /* @__PURE__ */ createComponent(VanillaRegularMasonryGrid)\n\nexport const BalancedMasonryGrid = /* @__PURE__ */ createComponent(VanillaBalancedMasonryGrid)\n\nexport interface FrameProps extends PolymorphicProps<'div'> {\n /**\n * Width of the frame.\n * Not necessarily real width, but used for aspect ratio calculation.\n */\n width: Signalish<number>\n /**\n * Height of the frame.\n * Not necessarily real height, but used for aspect ratio calculation.\n */\n height: Signalish<number>\n}\n\nexport const Frame = /* @__PURE__ */ polymorphic$((\n {\n as = div,\n width,\n height,\n style,\n ...props\n }: FrameProps,\n children\n) => (\n as({\n ...props,\n style: () => ({\n '--width': $get(width),\n '--height': $get(height),\n aspectRatio: 'var(--width) / var(--height)',\n ...$get(style)\n })\n })(\n ...children\n )\n))\n\nexport interface SpannedMasonryGridProps extends PolymorphicProps<'div'> {\n /**\n * Style for the inner container element.\n */\n innerStyle?: StyleValue\n /**\n * Minimum width of each frame of the grid.\n * If not provided, the grid will auto-fit as many columns as possible.\n */\n frameWidth?: Signalish<number | string | undefined>\n /**\n * Grid gap between items.\n */\n gap?: Signalish<number | string | undefined>\n /**\n * Precision for span calculation.\n * Affects how accurately frames will maintain their aspect ratios.\n * Higher precision results in more accurate sizes but may impact performance and cause bugs in some browsers.\n */\n precision?: Signalish<number | undefined>\n}\n\nconst DEFAULT_SPAN_PRECISION = 10\nconst SPANNED_INNER_STYLE: CSSProperties = {\n display: 'grid',\n clipPath: 'margin-box',\n margin: 'calc(-1 * var(--gap, 0px) / 2)',\n gridTemplateColumns: 'repeat(auto-fill, minmax(var(--frame-width), 1fr))'\n}\n\nexport const SpannedMasonryGrid = /* @__PURE__ */ polymorphic$((\n {\n as = div,\n precision,\n frameWidth,\n gap,\n style,\n innerStyle,\n ...props\n }: SpannedMasonryGridProps,\n children\n) => (\n as({\n ...props,\n style: () => ({\n '--frame-width': formatUnit($get(frameWidth)),\n '--gap': formatUnit($get(gap)),\n '--precision': $get(precision) ?? DEFAULT_SPAN_PRECISION,\n ...$get(style)\n })\n })(\n div({\n style: () => ({\n ...SPANNED_INNER_STYLE,\n ...$get(innerStyle)\n })\n })(\n ...children\n )\n )\n))\n\nexport interface SpannedFrameProps extends FrameProps {\n /**\n * Style for the inner container element.\n */\n innerStyle?: StyleValue\n}\n\nconst SPANNED_FRAME_STYLE: CSSProperties = {\n aspectRatio: 'var(--width) / var(--height)',\n width: '100%',\n height: '100%',\n position: 'relative',\n gridRow: 'span calc(var(--height) / var(--width) * var(--precision))'\n}\nconst SPANNED_FRAME_INNER_STYLE: CSSProperties = {\n position: 'absolute',\n inset: 'calc(var(--gap, 0px) / 2)'\n}\n\nexport const SpannedFrame = /* @__PURE__ */ polymorphic$((\n {\n as = div,\n width,\n height,\n style,\n innerStyle,\n ...props\n }: SpannedFrameProps,\n children\n) => (\n as({\n ...props,\n style: () => ({\n '--width': $get(width),\n '--height': $get(height),\n ...SPANNED_FRAME_STYLE,\n ...$get(style)\n })\n })(\n div({\n style: () => ({\n ...SPANNED_FRAME_INNER_STYLE,\n ...$get(innerStyle)\n })\n })(\n ...children\n )\n )\n))\n"],"mappings":"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"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,sBAI5D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@masonry-grid/nanoviews",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "A fast, lightweight, and responsive masonry grid layout library for Nanoviews.",
|
|
6
|
+
"author": "dangreen",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://masonry-grid.js.org",
|
|
9
|
+
"funding": "https://ko-fi.com/dangreen",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/TrigenSoftware/masonry-grid.git",
|
|
13
|
+
"directory": "packages/nanoviews"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/TrigenSoftware/masonry-grid/issues"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"masonry",
|
|
20
|
+
"grid",
|
|
21
|
+
"layout",
|
|
22
|
+
"masonry-grid",
|
|
23
|
+
"responsive",
|
|
24
|
+
"fast",
|
|
25
|
+
"lightweight",
|
|
26
|
+
"nanoviews"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=16"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"exports": {
|
|
33
|
+
"./package.json": "./package.json",
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"nanoviews": ">=1"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@masonry-grid/vanilla": "^1.1.1"
|
|
47
|
+
}
|
|
6
48
|
}
|