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

@micropackage/responsive-embeds

v1.0.3

Published

Makes any embed responsive, no markup changes required.

Downloads

589

Readme

Responsive Embeds

BracketSpace Micropackage npm License

🧬 About Responsive Embeds

This package provides a JavaScript function which can make any embed (like youtube video iframe) responsive.

💾 Installation

npm install --save @micropackage/responsive-embeds

or

yarn add @micropackage/responsive-embeds

🕹 Usage

import responsiveEmbeds from '@micropackage/responsive-embeds';

// See arguments description below.
responsiveEmbeds( element, params );

const iframe = document.getElementById( 'some-iframe' );
const embeds = document.querySelectorAll( '.responsive-embed' );

responsiveEmbeds( 'iframe[src*="youtube.com"]', {
  watch: true,
  wrapClass: 'my-custom-wrap',
} );
responsiveEmbeds( iframe );
responsiveEmbeds( embeds );

How it works?

This function wraps given element(s) in a div with relative position and padding-top set as percentage to match the original element's proportions. The element itself is positioned absolute inside this div with height and width set to 100%. This means the element will always match parent width also preserving it's original proportions.

This function can be used with any HTML element which initial proportions should be preserved.

⚙️ Arguments

This function has two arguments.

| Argument | Type | Description | |----------|-----------------------------------|------------------------------------------------------------------------------------------------------------------------------------------| | element | (string|HTMLElement|NodeList) | This can be either string selector which will be passed to document.querySelectorAll or an HTML node (HTMLElement) or NodeList instance. | | params | (object) | Additional params (see table below)(optional) |

Params

| Param | Type | Description | |---------------|-----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | wrapClass | (string) | Class name applied to the wrapping div. Default: 'responsive-embed-wrap' | | watch | (bool|string|HTMLElement) | When using string selector as a first argument, this param can be used to observe changes in the DOM and make responsive all the items which will be added later. If this is set to true entire document is observed. Alternatively a string selector or HTMLElement object can be set to observe only changes inside given element. Default: false |

Watching example

Let's say we have a WordPress theme with it's content wrapped in a div.entry-content element. We are using some plugin which loads youtube videos using youtube API, so the iframes might not be present in the DOM while we initialise responsiveEmbeds.

responsiveEmbeds( 'iframe[src*="youtube.com"]', {
  watch: '.entry-content',
} );

This call will start observing the DOM changes inside the div.entry-content and will make responsive any added node which will match the selector iframe[src*="youtube.com"]. It will also transform all the matching nodes which are already present in the DOM.

📦 About the Micropackage project

Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.

The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.

Micropackages are maintained by BracketSpace.

📖 Changelog

See the changelog file.

📃 License

GNU General Public License (GPL) v3.0. See the LICENSE file for more information.