Skip to content

Conversation

fabiot21
Copy link
Contributor

@fabiot21 fabiot21 commented Jul 28, 2023

closes #38

  • controller class
  • example
  • fix format
  • tests
  • update README

@cuire please review this PR and change whatever you want.

example of usage -> /examples/add-remove-item

<script lang="ts">
        import Grid, { GridItem, type GridController } from '$lib';
        
        let items = [
                { id: crypto.randomUUID(), x: 0, y: 0, w: 2, h: 5 },
                { id: crypto.randomUUID(), x: 2, y: 2, w: 2, h: 2 },
                { id: crypto.randomUUID(), x: 2, y: 0, w: 1, h: 2 },
                { id: crypto.randomUUID(), x: 2, y: 6, w: 3, h: 2 },
                { id: crypto.randomUUID(), x: 2, y: 4, w: 2, h: 2 }
        ];
        
        const itemSize = { height: 40 };
        
        let gridController: GridController;
              
        function addNewItem() {
                const w = Math.floor(Math.random() * 2) + 1;
                const h = Math.floor(Math.random() * 5) + 1;
                const newPosition = gridController.getFirstAvailablePosition(w, h);
                items = newPosition
	                ? [...items, { id: crypto.randomUUID(), x: newPosition.x, y: newPosition.y, w, h }]
	                : items;
        }
</script>

<button
	class="btn"
	on:click={addNewItem}>Add New Item</button
>

<Grid {itemSize} cols={10} collision="push" bind:controller={gridController}>
	{#each items as item (item.id)}
	    <GridItem id={item.id} bind:x={item.x} bind:y={item.y} bind:w={item.w} bind:h={item.h}>
		    <div class="item">{item.id.slice(0, 5)}</div>
	    </GridItem>
	{/each}
</Grid>

<style>
	/*..*/
</style>

@fabiot21 fabiot21 changed the title added helper + example [feat] gridHelper Jul 28, 2023
@fabiot21 fabiot21 marked this pull request as draft July 28, 2023 00:14
@fabiot21 fabiot21 changed the title [feat] gridHelper [feat] grid helper Jul 28, 2023
@cuire
Copy link
Owner

cuire commented Jul 28, 2023

I went on a short trip, will be back in 3-4 days.

@cuire
Copy link
Owner

cuire commented Jul 31, 2023

Cool demo I like it a lot 💖

@fabiot21 fabiot21 changed the title [feat] grid helper [feat] grid controller Jul 31, 2023
@fabiot21 fabiot21 requested a review from cuire July 31, 2023 21:17
@fabiot21 fabiot21 marked this pull request as ready for review July 31, 2023 23:18
@cuire
Copy link
Owner

cuire commented Aug 1, 2023

Awesome docs, smart way to export a type that used as an interface 😎

@cuire cuire merged commit 2d64a6e into cuire:main Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Helper to get available position

2 participants