GithubHelp home page GithubHelp logo

alpine-resize's Introduction


This package has been archived in favor of Alpine's Resize plugin.


NPM Version Build Size License

Alpine Resize

This is a convenience wrapper around ResizeObserver that allows you to easily run code whenever an element is resized. One possible use case is to dynamically set the sizes attribute on the <source> tag of a <picture> element.

Installation

You can use this plugin by either including it from a <script> tag or installing it via NPM:

Via CDN

You can include the CDN build of this plugin as a <script> tag, just make sure to include it BEFORE Alpine's core JS file.

<!-- Alpine Resize -->
<script defer src="https://cdn.jsdelivr.net/npm/@aerni/[email protected]/dist/cdn.min.js"></script>

<!-- Alpine Core -->
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

Via NPM

You can install Resize from NPM for use inside your bundle like so:

npm install @aerni/alpine-resize

Then initialize it from your bundle:

import Alpine from 'alpinejs'
import resize from '@aerni/alpine-resize'

Alpine.plugin(resize)

Usage

Add x-resize to any element within an Alpine component. The provided expression will execute as soon as the element is resized.

In this example, the element will show as soon as it is resized.

<div x-data="{ resized: false }" x-resize="resized = true">
    <div x-show="resized">
        This element shows as soon as it is resized
    </div>
</div>

You can also provide a callback to access the ResizeObserverEntry to do fancy things like changing an element's font size based on its width.

<div
    x-data="{
        width: 0,
        minFontSize: 15,
        maxFontSize: 50,
    }"
    x-resize="resizeObserverEntry => {
        width = resizeObserverEntry.contentRect.width
    }"
    :style="{
        'font-size': `${Math.min(maxFontSize, Math.max(minFontSize, width * 0.05))}px`,
    }"
>
    The font-size of this element will change based on its width
</div>

alpine-resize's People

Contributors

aerni avatar hirasso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

alpine-resize's Issues

Add feature detection for ResizeObserver

Hi again! I just had a thought: Shouldn't this directive probably do some feature detection before initializing the ResizeObserver? That way it would stay "progressive enhancement" and wouldn't break sites on older browsers. Anything like this:

if (typeof window.ResizeObserver !== 'function') return

or

if (!"ResizeObserver" in window) return;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.