Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loader state fixes #45 #57

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 80 additions & 26 deletions src/lib/components/Loader/Loader.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
<script lang="ts">
import {
IconCheckCircleFill, IconXCircleFill
} from '@hyvor/icons';

export let block : boolean = false;
export let full: boolean = false;
export let padding : 'none' | 'small' | 'medium' | 'large' | number = 'medium';

export let size : 'small' | 'medium' | 'large' | number = 'medium';

export let state : 'loading' | 'success' | 'error' | 'none' = 'loading';

export let duration : number = 2000;

$: {

if (state === 'success' || state === 'error') {
setTimeout(() => {
state = 'none';
}, duration);
}

}


export let color : string = 'var(--accent)';
export let colorTrack = 'var(--accent-lightest)';
Expand All @@ -17,6 +35,7 @@
colorTrack = colorCopy;
}


const sizes = {
small: 16,
medium: 24,
Expand Down Expand Up @@ -47,45 +66,64 @@
class="loader"
class:block
class:full
class:success={state === 'success'}
class:error={state === 'error'}


style:--local-size={size + "px"}
style:padding={block ? padding + "px" : undefined}
{...$$restProps}
>


<span class="loader-wrap">
<svg>
<circle
class="track"
cx="50%"
cy="50%"
r={r + "px"}
fill="none"
stroke-width={strokeWidth}
stroke={colorTrack}
></circle>
<circle
class="progress"
cx="50%"
cy="50%"
r={r + "px"}
fill="none"
stroke-width={strokeWidth}
stroke={color}
stroke-linecap="round"
stroke-dasharray={strokeDashArray}
stroke-dashoffset={strokeDashOffset}
></circle>
</svg>
</span>
{#if state !== 'none'}
<span class="loader-wrap">

{#if state === 'loading'}
<svg>
<circle
class="track"
cx="50%"
cy="50%"
r={r + "px"}
fill="none"
stroke-width={strokeWidth}
stroke={colorTrack}
></circle>
<circle
class="progress"
cx="50%"
cy="50%"
r={r + "px"}
fill="none"
stroke-width={strokeWidth}
stroke={color}
stroke-linecap="round"
stroke-dasharray={strokeDashArray}
stroke-dashoffset={strokeDashOffset}
></circle>
</svg>
{:else if state === 'success'}
<span class="success-icon">
<IconCheckCircleFill color="var(--green)" width={size} height={size} />
</span>
{:else if state === 'error'}
<span class="error-icon">
<IconXCircleFill color="var(--red)" width={size} height={size} />
</span>
{/if}

</span>

{/if}


{#if $$slots.default}
<div class="message">
<slot></slot>
</div>
{/if}

</div>


Expand Down Expand Up @@ -131,6 +169,10 @@
position: relative;
}

.success-icon, .error-icon {
animation: scale 0.2s ease-in-out;
}

svg {
width: inherit;
height: inherit;
Expand All @@ -156,4 +198,16 @@
}
}

@keyframes scale {
0% {
transform: scale(0.5);
opacity: 0.4;
}
100% {
transform: scale(1);
opacity: 1;
}
}


</style>
85 changes: 84 additions & 1 deletion src/routes/[[slug]]/docs/Loader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
import Button from "../../../lib/components/Button/Button.svelte";
import CodeBlock from "../../../lib/components/CodeBlock/CodeBlock.svelte";
import CodeResult from "./Helper/CodeResult.svelte";

let loaderStateS: 'loading' | 'none' | 'error' | 'success' = 'none';
let loaderStateE: 'loading' | 'none' | 'error' | 'success' = 'none';

function handleButtonClickS() {
loaderStateS = 'loading'

setTimeout(() => {
loaderStateS = 'success';
}, 2000)
}

function handleButtonClickE() {
loaderStateE = 'loading'

setTimeout(() => {
loaderStateE = 'error';
}, 2000)
}

</script>

<h1>Loader</h1>
Expand Down Expand Up @@ -54,6 +74,20 @@
</div>
</TableRow>

<TableRow>
<div><code>state</code></div>
<div><code>loading</code></div>
<div>
The state of the loader.
<ul>
<li><code>loading</code></li>
<li><code>success</code></li>
<li><code>error</code></li>
<li><code>none</code></li>
</ul>
</div>
</TableRow>

<TableRow>
<div><code>invert</code></div>
<div><code>false</code></div>
Expand Down Expand Up @@ -231,4 +265,53 @@

<CodeResult>
<Loader>Loading...</Loader>
</CodeResult>
</CodeResult>

<h3 id="state">States</h3>

<p>
You can set the <code>state</code> prop to <code>loading</code>, <code>success</code> or <code>error</code> to display the corresponding loaderState
</p>

<CodeBlock
code={`
<` + `script lang="ts">
let loaderStateS: 'loading' | 'none' | 'error' | 'success' = 'none';
let loaderStateE: 'loading' | 'none' | 'error' | 'success' = 'none';

function handleButtonClickS() {
loaderStateS = 'loading'

setTimeout(() => {
loaderStateS = 'success';
}, 2000)
}

function handleButtonClickE() {
loaderStateE = 'loading'

setTimeout(() => {
loaderStateE = 'error';
}, 2000)
}
<` + `/script>

<Button on:click={handleButtonClickS}>Toggle State Success
<Loader slot="action" size="small" state={loaderStateS} />
</Button>

<Button on:click={handleButtonClickE}>Toggle State Error
<Loader slot="action" size="small" state={loaderStateE} />
</Button>
`}
/>

<CodeResult white>
<Button on:click={handleButtonClickS} color="light">Success State
<Loader slot="action" size="small" state={loaderStateS} />
</Button>

<Button on:click={handleButtonClickE} color="light">Error State
<Loader slot="action" size="small" state={loaderStateE} />
</Button>
</CodeResult>