├── src ├── BlankTile.svelte ├── main.js ├── stores.js ├── layout.js ├── App.svelte ├── GridControl.svelte ├── GridTiles.svelte └── Tile.svelte └── README.md /src/BlankTile.svelte: -------------------------------------------------------------------------------- 1 | 3 | 4 |
5 |
6 | 7 | 9 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | import App from './App.svelte'; 2 | 3 | const app = new App({ 4 | target: document.body, 5 | props: { 6 | } 7 | }); 8 | 9 | export default app; 10 | -------------------------------------------------------------------------------- /src/stores.js: -------------------------------------------------------------------------------- 1 | import { writable } from 'svelte/store'; 2 | 3 | export let unlockTiles = writable(false); 4 | export let showDrops = writable(false); 5 | export let activeTile = writable(null); 6 | export let dropTarget = writable(null); 7 | export let dragOrigin = writable(null); 8 | 9 | export let currentLayout = writable([]); 10 | -------------------------------------------------------------------------------- /src/layout.js: -------------------------------------------------------------------------------- 1 | import BlankTile from "./BlankTile.svelte"; 2 | // import GridControl from "./GridControl.svelte"; 3 | //add your components here 4 | 5 | export let tileTypes = [ 6 | { name: 'blank tile', component: BlankTile} 7 | //add your components here if you want to be able to add them from the toolbar 8 | ] 9 | 10 | export let layout = [ 11 | // { "key": 1, 12 | // "type": GridControl, 13 | // "position": {"colStart": 1, "colEnd": 8, "rowStart": 1, "rowEnd": 3, "zIndex": 2005} 14 | // }, 15 | { "key": 2, 16 | "type": BlankTile, 17 | "position": {"colStart": 4, "colEnd": 36, "rowStart": 2, "rowEnd": 5, "zIndex": 2000} 18 | }, 19 | { "key": 4, 20 | "type": BlankTile, 21 | "position": {"colStart": 10, "colEnd": 16, "rowStart": 12, "rowEnd": 17, "zIndex": 2000} 22 | } 23 | ] 24 | -------------------------------------------------------------------------------- /src/App.svelte: -------------------------------------------------------------------------------- 1 | 5 | 6 |
7 |
8 | 9 |
10 | 11 |
12 | 13 |
14 | 15 |
16 | 17 | 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svelte-GridTiles 2 | Drag and drop resizable tiles on a responsive grid 3 | 4 | ------------------- 5 | 6 | - add a GridTiles component to your page
7 | - import your components in the layout.js file
8 | - hard code a startup layout or build one from an optional toolbar once the page loads
9 | - allow users to add particular components from the drop down, if you want 10 | - your components appear inside the Tiles!
11 | - svelte is awesome!
12 | 13 | - works in Firefox, Chrome, and Epiphany, so should work in Safari too 14 | - TODO: allow saving of custom layouts 15 | - TODO: enable touch interaction, currently mouse only 16 | 17 | check it out:
18 | at the REPL: 19 | https://svelte.dev/repl/b993c8f5fa9f4105aa9a246321025166?version=3.46.4 20 | 21 | or by getting yourself a svelte template: 22 | 23 | ```bash 24 | npx degit sveltejs/template my-svelte-project 25 | cd my-svelte-project 26 | npm install 27 | ``` 28 | then plop the files from this GridTiles repo into your new /src folder and run:
29 | ```bash 30 | npm run dev 31 | ``` 32 | 33 | [![screenshot_GridTiles](https://user-images.githubusercontent.com/98979350/152649391-5fcce1cf-8976-4eba-9f16-47e5347ad29f.png "screenshot_GridTiles")](https://user-images.githubusercontent.com/98979350/152649391-5fcce1cf-8976-4eba-9f16-47e5347ad29f.png "screenshot_GridTiles") 34 | -------------------------------------------------------------------------------- /src/GridControl.svelte: -------------------------------------------------------------------------------- 1 | 35 | 36 |
37 | 40 |
41 |
42 | 43 | 49 |
50 | 51 | 53 | -------------------------------------------------------------------------------- /src/GridTiles.svelte: -------------------------------------------------------------------------------- 1 | 70 | 71 | 72 | 73 | 74 |
75 | 76 | {#if $unlockTiles && $showDrops} 77 | {#each rowLooper as r} 78 | {#each colLooper as c} 79 | {#if $dragOrigin != r + "-" + c} 80 |
84 | 90 | 94 | target.setAttribute("fill", "black")} 95 | on:dragover|preventDefault={({ target }) => 96 | target.setAttribute("fill", "black")} 97 | on:dragleave={({ target }) => 98 | target.setAttribute("fill", "white")} 99 | on:mouseup={() => { 100 | showDrops.set(false); 101 | }} 102 | cx="6" 103 | cy="6" 104 | r="5" 105 | stroke="grey" 106 | stroke-width="1" 107 | fill="white" 108 | /> 109 | 110 |
111 | {/if} 112 | {/each} 113 | {/each} 114 | {/if} 115 | {#each $currentLayout as tile (tile.key)} 116 | 117 | cols ? -1 : tile.position.colEnd} 121 | rowEnd={tile.position.rowEnd > rows ? -1 : tile.position.rowEnd} 122 | zIndex={tile.position.zIndex} 123 | on:destroy={() => closeTile(tile.key)} 124 | on:adjust={(e) => adjustTile(e, tile.key)} 125 | > 126 | 127 | 128 | {/each} 129 |
130 | 131 | 164 | -------------------------------------------------------------------------------- /src/Tile.svelte: -------------------------------------------------------------------------------- 1 | 119 | 120 |
133 | {#if $unlockTiles} 134 |
135 |
136 | 137 | 145 | move 146 |
147 |
148 | 149 | handle_zAdjust(1)} 156 | /> 157 | raise 158 |
159 |
160 | 161 | handle_zAdjust(-1)} 168 | /> 169 | lower (z={zIndex}) 170 |
171 |
172 |
173 | close 174 | 175 | 183 | 184 |
185 |
reportResize(button, "resizeLeft")} 190 | on:dragend={resizeEnd} 191 | on:click={resizeEnd} 192 | transition:fade 193 | > 194 | 195 | 196 | resize 197 |
198 |
reportResize(button, "resizeRight")} 203 | on:dragend={resizeEnd} 204 | on:click={resizeEnd} 205 | transition:fade 206 | > 207 | resize 208 | 209 | 210 | 211 |
212 | {/if} 213 | 214 |
215 | 216 | 293 | --------------------------------------------------------------------------------