Skip to content

Commit 233b9b2

Browse files
committed
docs: add more svelte examples
1 parent 6f3925f commit 233b9b2

File tree

21 files changed

+587
-3
lines changed

21 files changed

+587
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script lang="ts">
2+
import { BalancedMasonryGrid, Frame } from '@masonry-grid/svelte'
3+
import Controls from './Controls.svelte'
4+
import { createItems } from './utils'
5+
6+
let itemsCount = $state(35)
7+
let frameWidth = $state(200)
8+
let gap = $state(10)
9+
let containerWidth = $state(100)
10+
11+
let items = $derived(createItems(itemsCount))
12+
</script>
13+
14+
<div class="container">
15+
<h1>Balanced Masonry Grid - Svelte</h1>
16+
17+
<Controls
18+
bind:itemsCount
19+
bind:frameWidth
20+
bind:gap
21+
bind:containerWidth
22+
/>
23+
24+
<BalancedMasonryGrid
25+
class="container"
26+
{gap}
27+
{frameWidth}
28+
style="width: {containerWidth}%"
29+
>
30+
{#each items as item, i (i)}
31+
<Frame
32+
class="frame"
33+
width={item.width}
34+
height={item.height}
35+
style="background-color: {item.backgroundColor}"
36+
>
37+
{i + 1}
38+
</Frame>
39+
{/each}
40+
</BalancedMasonryGrid>
41+
</div>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script lang="ts">
2+
export let itemsCount = 35
3+
export let frameWidth = 200
4+
export let gap = 10
5+
export let containerWidth = 100
6+
</script>
7+
8+
<div class="controls">
9+
<div class="control-group">
10+
<label for="itemsCount">Items Count: {itemsCount}</label>
11+
<input
12+
id="itemsCount"
13+
type="range"
14+
min="3"
15+
max="100"
16+
bind:value={itemsCount}
17+
/>
18+
</div>
19+
20+
<div class="control-group">
21+
<label for="frameWidth">Frame Width: {frameWidth}px</label>
22+
<input
23+
id="frameWidth"
24+
type="range"
25+
min="50"
26+
max="300"
27+
bind:value={frameWidth}
28+
/>
29+
</div>
30+
31+
<div class="control-group">
32+
<label for="gap">Gap: {gap}px</label>
33+
<input
34+
id="gap"
35+
type="range"
36+
min="0"
37+
max="40"
38+
bind:value={gap}
39+
/>
40+
</div>
41+
42+
<div class="control-group">
43+
<label for="containerWidth">Container Width: {containerWidth}%</label>
44+
<input
45+
id="containerWidth"
46+
type="range"
47+
min="0"
48+
max="100"
49+
bind:value={containerWidth}
50+
/>
51+
</div>
52+
</div>

examples/svelte-balanced/index.css

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
* {
2+
box-sizing: border-box;
3+
margin: 0;
4+
padding: 0;
5+
}
6+
7+
body {
8+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
9+
padding: 20px;
10+
background: #f5f5f5;
11+
}
12+
13+
.container {
14+
max-width: 1200px;
15+
margin: 0 auto;
16+
}
17+
18+
h1 {
19+
margin-bottom: 20px;
20+
color: #333;
21+
}
22+
23+
.controls {
24+
margin-bottom: 30px;
25+
padding: 20px;
26+
background: white;
27+
border-radius: 8px;
28+
display: grid;
29+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
30+
gap: 20px;
31+
}
32+
33+
.control-group {
34+
display: flex;
35+
flex-direction: column;
36+
gap: 8px;
37+
}
38+
39+
.control-group label {
40+
font-size: 14px;
41+
font-weight: 500;
42+
color: #555;
43+
}
44+
45+
.control-group input[type="range"] {
46+
width: 100%;
47+
height: 6px;
48+
border-radius: 3px;
49+
background: #e0e0e0;
50+
outline: none;
51+
cursor: pointer;
52+
}
53+
54+
.control-group input[type="range"]::-webkit-slider-thumb {
55+
-webkit-appearance: none;
56+
appearance: none;
57+
width: 16px;
58+
height: 16px;
59+
border-radius: 50%;
60+
background: #007bff;
61+
cursor: pointer;
62+
}
63+
64+
.control-group input[type="range"]::-moz-range-thumb {
65+
width: 16px;
66+
height: 16px;
67+
border-radius: 50%;
68+
background: #007bff;
69+
cursor: pointer;
70+
border: none;
71+
}
72+
73+
.frame {
74+
display: flex;
75+
align-items: center;
76+
justify-content: center;
77+
font-size: 18px;
78+
font-weight: bold;
79+
color: rgba(0, 0, 0, 0.3);
80+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Balanced Masonry Grid - Svelte</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="/main.ts"></script>
11+
</body>
12+
</html>

examples/svelte-balanced/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { mount } from 'svelte'
2+
import App from './App.svelte'
3+
import './index.css'
4+
5+
mount(App, {
6+
target: document.getElementById('app')!
7+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "module",
3+
"scripts": {
4+
"dev": "vite"
5+
},
6+
"dependencies": {
7+
"@masonry-grid/svelte": "^1.0.0",
8+
"svelte": "^5.0.0"
9+
},
10+
"devDependencies": {
11+
"@sveltejs/vite-plugin-svelte": "^6.2.1",
12+
"typescript": "^5.3.3",
13+
"vite": "^7.0.0"
14+
}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
2+
3+
export default {
4+
preprocess: vitePreprocess()
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
5+
"module": "ESNext",
6+
"skipLibCheck": true,
7+
8+
"moduleResolution": "bundler",
9+
"allowImportingTsExtensions": true,
10+
"isolatedModules": true,
11+
"moduleDetection": "force",
12+
"noEmit": true,
13+
14+
"strict": true,
15+
"noUnusedLocals": true,
16+
"noUnusedParameters": true,
17+
"noFallthroughCasesInSwitch": true
18+
},
19+
"include": ["."]
20+
}

examples/svelte-balanced/utils.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export type AspectRatio = [number, number]
2+
3+
const aspectRatios: AspectRatio[] = [
4+
[1, 1],
5+
[2, 3],
6+
[3, 2]
7+
]
8+
9+
export function getRandomAspectRatio(prevAspectRatio?: AspectRatio): AspectRatio {
10+
const newAspectRatio =
11+
aspectRatios[Math.floor(Math.random() * aspectRatios.length)]
12+
13+
if (newAspectRatio === prevAspectRatio) {
14+
return getRandomAspectRatio(prevAspectRatio)
15+
}
16+
17+
return newAspectRatio
18+
}
19+
20+
export function getRandomColor(): string {
21+
return `hsl(${Math.floor(Math.random() * 360)}, 100%, 75%)`
22+
}
23+
24+
export interface Item {
25+
width: number
26+
height: number
27+
backgroundColor: string
28+
}
29+
30+
export function createItems(itemsCount: number): Item[] {
31+
let aspectRatio: AspectRatio
32+
33+
return Array.from({
34+
length: itemsCount
35+
}, () => {
36+
aspectRatio = getRandomAspectRatio(aspectRatio)
37+
38+
const backgroundColor = getRandomColor()
39+
40+
return {
41+
width: aspectRatio[0],
42+
height: aspectRatio[1],
43+
backgroundColor
44+
}
45+
})
46+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from 'vite'
2+
import { svelte } from '@sveltejs/vite-plugin-svelte'
3+
4+
export default defineConfig({
5+
plugins: [svelte()]
6+
})

0 commit comments

Comments
 (0)