feat(component): add Slider

Closes #26
This commit is contained in:
Eric Liu 2019-12-29 12:25:14 -08:00
commit 50fbb60f3c
16 changed files with 253 additions and 11 deletions

View file

@ -0,0 +1,23 @@
<script>
let className = undefined;
export { className as class };
export let hideLabel = false;
export let style = undefined;
import { cx } from '../../lib';
</script>
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}>
{#if !hideLabel}
<span class={cx('--label', '--skeleton')} />
{/if}
<div class={cx('--slider-container', '--skeleton')}>
<span class={cx('--slider__range-label')} />
<div class={cx('--slider')}>
<div class={cx('--slider__track')} />
<div class={cx('--slider__filled-track')} />
<div class={cx('--slider__thumb')} />
</div>
<span class={cx('--slider__range-label')} />
</div>
</div>

View file

@ -0,0 +1,23 @@
<script>
export let story = undefined;
import Layout from '../../internal/ui/Layout.svelte';
import Slider from './Slider.svelte';
import SliderSkeleton from './Slider.Skeleton.svelte';
let value = 50;
</script>
<Layout>
{#if story === 'skeleton'}
<SliderSkeleton {...$$props} />
{:else}
<Slider
{...$$props}
id="slider"
bind:value
on:change={({ detail }) => {
console.log('on:change', detail);
}} />
{/if}
</Layout>

View file

@ -0,0 +1,35 @@
import { withKnobs, text, boolean, number } from '@storybook/addon-knobs';
import Component from './Slider.Story.svelte';
export default { title: 'Slider', decorators: [withKnobs] };
export const Default = () => ({
Component,
props: {
name: text('Form item name (name)', ''),
inputType: text('The form element type (inputType)', 'number'),
ariaLabelInput: text(
'The ARIA label for the <input> (ariaLabelInput)',
'Label for slider value'
),
disabled: boolean('Disabled (disabled)', false),
light: boolean('Light variant (light)', false),
hideTextInput: boolean('Without text input (hideTextInput)', false),
min: number('The minimum value (min)', 0),
max: number('The maximum value (max)', 100),
step: number('The step (step)', 1),
stepMuliplier: number('The step factor for Shift+arrow keys (stepMuliplier)', 4),
labelText: text('Label text (labelText)', 'Slider Label'),
minLabel: text('Label for minimum value (minLabel)', ''),
maxLabel: text('Label for maximum value (maxLabel)', ''),
hideLabel: boolean('Hide label (hideLabel)', false)
}
});
export const Skeleton = () => ({
Component,
props: {
story: 'skeleton',
hideLabel: boolean('Hide label (hideLabel)', false)
}
});

View file

@ -0,0 +1,152 @@
<script>
let className = undefined;
export { className as class };
export let hideTextInput = false;
export let id = Math.random();
export let value = '';
export let min = 0;
export let max = 100;
export let minLabel = '';
export let maxLabel = '';
export let labelText = '';
export let formatLabel = (value, label) => {
return typeof label === 'function' ? label(value) : `${value}${label}`;
};
export let step = 1;
export let stepMultiplier = 4;
export let disabled = false;
export let name = '';
export let inputType = 'number';
export let invalid = false;
export let required = false;
export let light = false;
export let style = undefined;
import { createEventDispatcher, afterUpdate, tick } from 'svelte';
import { cx } from '../../lib';
const dispatch = createEventDispatcher();
let dragging = false;
let holding = false;
let elementRef = undefined;
let trackRef = undefined;
function startDragging() {
dragging = true;
}
function startHolding() {
holding = true;
}
function stopHolding() {
holding = false;
}
function calcValue(event) {
const offsetX = event.touches ? event.touches[0].clientX : event.clientX;
const { left, width } = trackRef.getBoundingClientRect();
let nextValue = min + Math.round(((max - min) * ((offsetX - left) / width)) / step) * step;
if (nextValue <= min) {
nextValue = min;
} else if (nextValue >= max) {
nextValue = max;
}
value = nextValue;
}
afterUpdate(() => {
if (!holding) {
dispatch('change', value);
}
});
$: range = max - min;
$: left = ((value - min) / range) * 100;
$: {
if (value <= min) {
value = min;
} else if (value >= max) {
value = max;
}
if (dragging) {
calcValue(event);
dragging = false;
}
}
</script>
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item', className)} {style}>
<label for={id} class={cx('--label', disabled && '--label--disabled')}>{labelText}</label>
<div class={cx('--slider-container')}>
<span class={cx('--slider__range-label')}>{formatLabel(min, minLabel)}</span>
<div
bind:this={elementRef}
role="presentation"
tabindex="-1"
class={cx('--slider', disabled && '--slider--disabled')}
on:click={startDragging}
on:mousemove={() => {
if (holding) {
startDragging();
}
}}
on:touchmove={() => {
if (holding) {
startDragging();
}
}}
on:mouseup={stopHolding}
on:touchup={stopHolding}
on:touchend={stopHolding}
on:touchcancel={stopHolding}>
<div
role="slider"
tabindex="0"
class={cx('--slider__thumb')}
style={`left: ${left}%`}
aria-valuemax={max}
aria-valuemin={min}
aria-valuenow={value}
on:mousedown={startHolding}
on:touchstart={startHolding}
on:keydown={({ shiftKey, key }) => {
const keys = { ArrowDown: -1, ArrowLeft: -1, ArrowRight: 1, ArrowUp: 1 };
if (keys[key]) {
value += step * (shiftKey ? range / step / stepMultiplier : 1) * keys[key];
}
}}
{id} />
<div bind:this={trackRef} class={cx('--slider__track')} />
<div
class={cx('--slider__filled-track')}
style={`transform: translate(0, -50%) scaleX(${left / 100})`} />
<input
type="hidden"
class={cx('--slider__input')}
{name}
{value}
{required}
{min}
{max}
{step} />
</div>
<span class={cx('--slider__range-label')}>{formatLabel(max, maxLabel)}</span>
{#if !hideTextInput}
<input
type={inputType}
id={`input-${id}`}
aria-label={$$props['aria-label'] || 'Slider number input'}
class={cx('--text-input', '--slider-text-input', light && '--text-input--light', invalid && '--text-input--invalid')}
on:change={({ target }) => {
value = Number(target.value);
}}
{disabled}
{value} />
{/if}
</div>
</div>

View file

@ -0,0 +1,4 @@
import Slider from './Slider.svelte';
export default Slider;
export { default as SliderSkeleton } from './Slider.Skeleton.svelte';