Skip to content

Commit

Permalink
fix directory picker: value exported, make use of svelte:window, chan…
Browse files Browse the repository at this point in the history
…ges classes to fix possible conflicts
  • Loading branch information
ArmanNik committed Nov 26, 2024
1 parent 04ff0b8 commit 41e89b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
21 changes: 13 additions & 8 deletions v2/pink-sb/src/lib/DirectoryPicker/DirectoryItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
export let directories: Directory[];
export let level = 0;
export let containerWidth: number | undefined;
export let value: string;
let radioInputs: HTMLInputElement[] = [];
let thumbnailStates = directories.map(() => ({
loading: true,
error: false
}));
function handleThumbnailLoad(index) {
function handleThumbnailLoad(index: number) {
thumbnailStates[index].loading = false;
thumbnailStates[index].error = false;
}
function handleThumbnailError(index) {
function handleThumbnailError(index: number) {
thumbnailStates[index].loading = false;
thumbnailStates[index].error = true;
}
Expand All @@ -39,7 +40,7 @@
{@const hasChildren = !!children?.length}
{@const thumb = thumbnailUrl}

<div class="container">
<div class="directory-item-container">
<button
class="folder"
style={paddingLeftStyle}
Expand All @@ -55,7 +56,8 @@
<Radio
group="directory"
name="directory"
size="small"
size="s"
bind:value
bind:radioInput={radioInputs[i]}
/>
<div
Expand All @@ -68,10 +70,13 @@
<div class="meta">
<span
class="title"
style={containerWidth &&
`max-width: ${containerWidth - 100 - level * 40}px`}>{title}</span
style={containerWidth
? `max-width: ${containerWidth - 100 - level * 40}px`
: ''}>{title}</span
>
<span class="fileCount">({fileCount} files)</span>
{#if fileCount !== undefined}
<span class="fileCount">({fileCount} files)</span>
{/if}
</div>
</div>
<div class="thumbnail-container">
Expand Down Expand Up @@ -103,7 +108,7 @@
{/each}

<style>
.container {
.directory-item-container {
width: 100%;
}
.folder {
Expand Down
30 changes: 15 additions & 15 deletions v2/pink-sb/src/lib/DirectoryPicker/DirectoryPicker.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { createTreeView } from '@melt-ui/svelte';
import { setContext } from 'svelte';
import { onMount, setContext } from 'svelte';
import type { Directory } from './index.js';
import DirectoryItem from './DirectoryItem.svelte';
Expand All @@ -17,19 +17,13 @@
export let directories: Directory[];
export let isLoading = true;
export let value: string;
let rootContainer: HTMLDivElement;
let containerWidth: number | undefined;
monitorResize();
function monitorResize() {
window.addEventListener('resize', updateWidth);
return {
destroy() {
window.removeEventListener('resize', updateWidth);
}
};
}
onMount(() => {
updateWidth();
});
function updateWidth() {
containerWidth = rootContainer ? rootContainer.getBoundingClientRect().width : undefined;
Expand All @@ -38,14 +32,20 @@
$: containerWidth = rootContainer ? rootContainer.getBoundingClientRect().width : undefined;
</script>

<div class="container" class:isLoading {...$tree} bind:this={rootContainer}>
{#if isLoading}<div class="loading-container">
<svelte:window on:resize={updateWidth} />

<div class="directory-container" class:isLoading {...$tree} bind:this={rootContainer}>
{#if isLoading}
<div class="loading-container">
<Spinner /><span>Loading directory data...</span>
</div>{:else}<DirectoryItem {directories} {containerWidth} />{/if}
</div>
{:else}
<DirectoryItem {directories} {containerWidth} bind:value />
{/if}
</div>

<style>
.container {
.directory-container {
width: 560px;
max-width: 100%;
height: 316px;
Expand Down
2 changes: 1 addition & 1 deletion v2/pink-sb/src/lib/DirectoryPicker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export type Directory = {
id: string;
title: string;
fullPath: string;
fileCount: number;
thumbnailUrl: string;
fileCount?: number | undefined;
children?: Directory[];
};

Expand Down

0 comments on commit 41e89b3

Please sign in to comment.