Giter Site home page Giter Site logo

micropackage / responsive-embeds Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 1.0 468 KB

Simple JavaScript function to make any embed responsive

License: GNU General Public License v3.0

JavaScript 100.00%
micropackage bracketspace dom javascript-library node-module

responsive-embeds's Introduction

Responsive Embeds

BracketSpace Micropackage npm License

Micropackage logo

๐Ÿงฌ 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.

responsive-embeds's People

Contributors

jakubmikita avatar szaleq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

rivman

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.