0.2.26 • Published 1 year ago

svelte-helpers v0.2.26

Weekly downloads
55
License
ISC
Repository
github
Last release
1 year ago

svelte-helpers


Installation

npm i svelte-helpers --save


This library contains some helpers I've found useful for working with Svelte.

Spring helpers

svelte-helpers/spring-transitions has functions to help you use spring animations in an enter or exit transition.

springIn((from: number), (to: number), (springConfig: object));
springOut((from: number), (to: number), (springConfig: object));

Both of these functions return an object with a duration, and tickToValue property, for use in a Svelte transition. Duration is the duration of the given spring, while tickToValue is a lookup function that will return the spring's actual value for a given value of t in the transition's css function. For example:

import { springIn, springOut } from "svelte-helpers/spring-transitions";

const SPRING_IN = { stiffness: 0.1, damping: 0.1 };
const animateIn = node => {
  const { duration, tickToValue } = springIn(-80, 0, SPRING_IN);
  return {
    duration: duration,
    css: t => `transform: translateY(${tickToValue(t)}px)`
  };
};

const SPRING_OUT = { stiffness: 0.1, damping: 0.5, precision: 3 };
const animateOut = node => {
  const current = currentYTranslation(node);
  const { duration, tickToValue } = springOut(
    current ? current : 0,
    80,
    SPRING_OUT
  );

  return {
    duration: duration,
    css: t => `transform: translateY(${tickToValue(t)}px)`
  };
};

{#if open}
  <div in:animateIn out:animateOut class="box">
    Hello World!
  </div>
{/if}

Modal

A declarative modal component. Receives an open prop that, when true, renders the modal with an overlay behind it. The modal fires a close event when the user clicks outside the modal, or presses escape. The handler should ultimately cause your open prop to become false.

The modal and overlay will both animate in and out. These animations are not (yet) configurable.

<script>
  import Modal from "svelte-helpers/Modal";

  let modalOpen = false;
  const closeModal1 = () => (modalOpen = false);
</script>

<button on:click={openModal1}>Show Modal</button>

<Modal open={modalOpen} on:close={closeModal1}>
  <h1>Hi there</h1>
  <button on:click={() => (modalOpen = false)}>Close</button>
</Modal>

Live Demo

Here

Props

PropTypeDescription
openboolWhether the modal is open
on:closefunctionCalled when the user clicks outside the modal, or presses escape. This should set state so the open prop becomes false
useContentWidthboolBy default, the modal has a set, responsive width (that's overridable with css). Set this to true to have the modal's width size freely with your content
animateResizingboolBy default changes to the modal's dimensions will animate. Pass false to turn this off.
deferStateChangeOnCloseboolUsed for conditionally rendering the modal itself. See below
modalKeystringUsed for conditionally rendering the modal itself. See below

Events

NamePurpose
on:closeThe modal has been closed. Use this method to set whatever state to reflect this closed state
on:closingThe modal's closing animation has started.
on:closedThe modal's closing animation has completed.
on:mountThe open modal's content has been mounted in the dom. Use this event to focus an input, etc

The Modal also has a closeModal method you can bind to, and call as needed.

{#if open3a}
  <Modal modalKey="modal3a" open={true} deferStateChangeOnClose={true} bind:closeModal={closeModal3a} on:close={() => (open3a = false)}>
    <button on:click={closeModal3a}>Close</button>
  </Modal>
{/if}

This can be used whether conditionally rendering (explained below), or not.

Conditional Rendering

If you need to conditionally render the modal, ie something like this

<script>
  import Modal from "svelte-helpers/Modal.svelte";
  let closeThisModal;
</script>

{#if open3}
  <Modal modalKey="modal3" open={true} deferStateChangeOnClose={true} on:close={() => (open3 = false)} useContentWidth={true} bind:closeModal={closeThisModal}>
    <ModalDemo2 />
  </Modal>
{/if}

Pass true to the deferStateChangeOnClose prop. Doing this will cause the close behavior of the modal to first run the standard exit animation, and then, once the modal is gone, fire the close event, to reset your state.

If you need to imperatively close a modal that's conditionally rendered, bind to the closeModal method, described above, or if inside a component rendered by the Modal, just grab the closeModal method from context, described below. If (and only if) conditionally rendering your modal, you need to use one of these two methods to close your modal imperatively. Do not just reset your state, since this will immediately unmount the modal's content, without the proper unmount animations.

Context

Within a modal there is a svelte-helpers-modal context value that's an object, with the following properties

PropTypeDescription
closeModalfunctionCloses the current modal. Can be used regardless of whether the modal is conditionally rendered.
isAnimatingResizingwritable<bool>Writable store of a boolean value, that controls whether changes to the modal's dimensions are animated. This defaults to true, or whatever you pass to animateResizing, but can be changed any time. For example, you might flip it to false while running your own animations in the modal's content, and revert it to true when finished.

Style Overrides

The modal has a global css class of svelte-helpers-modal-content applied. Add your own overrides as needed.

AutoSuggest

<script>
  import AutoSuggest from "svelte-helpers/AutoSuggest";
</script>

<AutoSuggest filterField={'name'} displayField="name" placeholder="Search" {options}>
  <div slot="result" let:option>{option.name}</div>
  <span slot="no-results" style="color: blue">No Results, yo</span>
</AutoSuggest>

Live Demo

Here

Props

PropType                                                               Description
options[object] or [string] or a function return either of thoseThe options to show. To disable an option, give it a disabled property set to true. If you pass a function returning your options, the options will not change when the results list is open. This can be a handy way to prevent your options list from jumping the moment you select one, if such a selection changes which options can be selected.
placeholderstringThe input's placeholder
inputStylesstringThe input's styles, if any
inputPropsobjectAny props (attributes) you'd like to apply to the input
currentSearchstringThe currently searched for text. Bind to this if you'd like to control the input's value
onItemSelected(item, inputElement) => voidOptional callback to call when an item is selected. By default the input will fill its value with the selected item's displayField if it's an object, or the item itself if it's a string. If this callback is provided, it will be called and nothing else will happen.
onBlur() => voidOptional callback to call when the input loses focus
filterFieldstringIf options are objects, this specifies the prop to filter by
noFilteringbooleanIf true, no filtering will happen. Filter your options however you'd like, and pass them in
keyFieldstringIf options are objects, this specifies the key prop for Svelte's #each block. Array index will be used if not provided.
displayFieldstringIf options are objects, this specifies the prop to display when selected
filterByStartsWithboolean = falseIf true the options will filter based on a match from the start of the option. Otherwise options will filter based on a match anywhere in the option's text

Slots

result - the slot for each option the user is viewing no-results - the slot to show when there are no results to display

CSS Overrides

The following CSS variables can be overridden to customize this component's appearance

VarPurpose
--svelte-helpers-auto-complete-border-colorBorder color for the input and options container
--svelte-helpers-auto-complete-border-widthBorder width for the input and options container
--svelte-helpers-auto-complete-border-radiusBorder radius for the input and options container
--svelte-helpers-auto-complete-option-paddingPadding of each result option
--svelte-helpers-auto-complete-results-max-heightOptions container max height
--svelte-helpers-auto-complete-option-hover-backgroundOption background color when hovered
--svelte-helpers-auto-complete-option-cursorCursor for options
--svelte-helpers-auto-complete-options-background-colorOptions container background color
--svelte-helpers-auto-complete-disabled-option-cursorCursor for disabled options
0.2.26

1 year ago

0.2.25

1 year ago

0.2.24

1 year ago

0.2.23

1 year ago

0.2.22

1 year ago

0.2.21

1 year ago

0.2.20

1 year ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.14

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.3

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.2

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.0-beta24

3 years ago

0.2.0-beta15

3 years ago

0.2.0-beta14

3 years ago

0.2.0-beta13

3 years ago

0.2.0-beta19

3 years ago

0.2.0-beta18

3 years ago

0.2.0-beta17

3 years ago

0.2.0-beta16

3 years ago

0.2.0-beta22

3 years ago

0.2.0-beta21

3 years ago

0.2.0-beta20

3 years ago

0.2.0-beta23

3 years ago

0.2.0-beta12

3 years ago

0.2.0-beta10

3 years ago

0.2.0-beta9

3 years ago

0.2.0-beta8

3 years ago

0.2.0-beta7

3 years ago

0.2.0-beta6

3 years ago

0.2.0-beta5

3 years ago

0.2.0-beta4

3 years ago

0.2.0-beta3

3 years ago

0.2.0-beta2

3 years ago

0.1.1-beta7

3 years ago

0.2.0-beta1

3 years ago

0.1.1-beta5

3 years ago

0.1.1-beta6

3 years ago

0.1.1-beta4

3 years ago

0.1.1-beta3

3 years ago

0.1.1-beta2

3 years ago

0.1.1-beta1

3 years ago

0.1.0

3 years ago

0.0.1-beta31

3 years ago

0.0.1-beta30

3 years ago

0.0.1-beta29

3 years ago

0.0.1-beta28

3 years ago

0.0.1-beta27

3 years ago

0.0.1-beta26

3 years ago

0.0.1-beta24

3 years ago

0.0.1-beta23

3 years ago

0.0.1-beta19

3 years ago

0.0.1-beta18

3 years ago

0.0.1-beta21

3 years ago

0.0.1-beta20

3 years ago

0.0.1-beta22

3 years ago

0.0.1-beta17

3 years ago

0.0.1-beta16

3 years ago

0.0.1-beta15

3 years ago

0.0.1-beta14

3 years ago

0.0.1-beta13

3 years ago

0.0.1-beta12

3 years ago

0.0.1-beta11

3 years ago

0.0.1-beta10

3 years ago

0.0.1-beta9

3 years ago

0.0.1-beta8

3 years ago

0.0.1-beta7

3 years ago

0.0.1-beta5

3 years ago

0.0.1-beta6

3 years ago

0.0.1-beta4

3 years ago

0.0.1-beta3

3 years ago

0.0.1-beta2

3 years ago

0.0.1-beta1

3 years ago

0.0.1-beta

4 years ago