npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@arisbh/elaimant

v1.0.4

Published

Elaimant is a simple Sveltekit compoment that applies a magnetic attraction effect to the provided element it wraps, based on the mouse position.

Downloads

19

Readme

What does it do ?

Elaimant is a simple Sveltekit compoment that applies a magnetic attraction effect to the provided element it wraps, based on the mouse position.

Here's a quick demo/playground in Svelte REPL

Installation

Use your preferred node package manager.

npm i @arisbh/elaimant

pnpm add @arisbh/elaimant

yarn add @arisbh/elaimant

Usage

Import the Elaimant component, and wrap your content with it.

<script>
	import Elaimant from '@arisbh/elaimant';
</script>

<Elaimant>
	<!-- ... Your content -->
</Elaimant>

Props

Attraction zone

To show the zone where your cursor will start magnetising your element, simple use the attractionZone prop.

<Elaimant attractionZone>
	<!-- ... Your content -->
</Elaimant>

Options

To customize Elaimant behaviour, you can pass your options in two ways.

Directly inside the Elaimant component, with the options prop (Typescript autosuggestions enabled).

<Elaimant
	options={{
		triggerDist: 75,
		speed: 300
		//...
	}}
>
	<!-- ... Your content -->
</Elaimant>

Or construct an object with the type ElaimantOptions, and use it inside the component.

import Elaimant, type { ElaimantOptions } from '@arisbh/elaimant';

const options: ElaimantOptions = {
	triggerDist: 75,
	speed: 300
	//...
}
<Elaimant {options}>
	<!-- ... Your content -->
</Elaimant>

Here are the default options when none are passed to the Elaimant components.

| Props | Default | Type | Description (WIP) | | -------------- | ---------- | --------------------------------- | ---------------------------------------------------------------------------- | | triggerDist | 75 | number in pixel | The minimal distance at which the element is attracted | | speed | 300 | number in millisecond | The speed the element will be attracted, magnet force if you want | | easing | ease-out | string, any CSS easing function | The easing function the element will follow upon movement | | mode | 'circle' | 'circle' or 'block' | View "In depth" section below | | dampenAmount | 2 | number | The factor of movement the element will go (1 to be completely on the mouse) | | mouseOnly | true | boolean | View "In depth" section below | | debug | false | boolean | Logs debugging informations in the console |

In depth

mode

Elaimant provides two mode to interpret the attraction zone:

  • circle : This create a simple circle from the center of your element, with paddings set to the trigger distance. It is better suited for squarish elements.
  • block : This create a box around your component, taking into account its size and the trigger distance. It is better suited for elements with eneven width and height.

mouseOnly

Smartphones browsers do not have a hover state on elements, thus, this effect is not quite appropriate. By default, if the module detects unsupported hover, no event listener will be added to your content, keeping the calculations to zero.

Overwise, if you still want to force the effect, you can with this options. Your content will move toward your touch position inside the attraction zone, and released when something else is touch.

Events

Directives

Elaimant comes with two events to interact with it.

  • on:attracted triggers once when the mouse enters the attraction zone.
  • on:released triggers once when the mouse leaves the attraction zone.
const handleElaimant = (e: CustomEvent) => {
	const { slotted, options } = e.detail;
};
<Elaimant on:attracted={handleElaimant} on:released={handleElaimant}>
	<!-- ... Your content -->
</Elaimant>

Both of these will return an event prop, containing a reference to your slotted elements and given options.

Variable

It also bubbles up the attracted boolean for added flexibility inside your content,

<Elaimant let:attracted>
	<YourContent>
		isAttracted: {attracted}
	</YourContent>
</Elaimant>

Styling

Elaimant is nearly style-free, and should not interfere with your setup.

However, it adds a [data-attracted] attribute to your elements when they are attracted, allowing you to style them how you want.

:global([data-attracted='true']) {
	outline: 1px solid hsl(var(--primary));
	transform: scale(1.2);
}

AttractionZone

You can use the class directive on Elaimant to specify dedicated CSS variables. Make sure to pass correct CSS rules.

Be aware that using custom CSS properties will wrap your component into another div. This is default Svelte behaviour.

<Elaimant attractionZone --zone-border={'2px solid hsl(var(--primary))'} --zone-bg={'red'}>
	<!-- ... Your content -->
</Elaimant>

You can also style them globaly defining them in your :root CSS selector.

To give you the most versatile behaviour, the [data-attracted] attribute is also added to the attraction zone. Make sure your to target them indivually when necessary.

Caveats

  • I am aware of the 'orthogonal/cross' movement when using mode: 'block'.

    This is because the distance is calculated from the mouse position to the closest border of your element (top, bottom, left or right). Until I can figure how to do it from the closest point on the perimeter, I have no way to fix this. Fortunately, I believe mode: 'circle' will mostly be used.

If your encountered other bugs, feel free to open an issue on the Github page !