-
Notifications
You must be signed in to change notification settings - Fork 0
Home
A collection of documented vanilla JS widgets.
In the A17 Boilerplate, we use these as a dependency and install via NPM. Alternatively you can copy the src
folder and the index.js
into a folder of your project and compile.
If installed via NPM, you'll want to import into your JavaScript by:
import helpers from '@area17/a17-helpers'
or, cherry pick individual helpers:
import { getCurrentMediaQuery, resized } from '@area17/a17-helpers'
- ajaxRequest - Performs ajax requests
- cookieHandler - cookie helpers
- copyTextToClipboard - Copies text to clipboard
- debounce - Debounced function call
- escapeString - Escapes and URL encodes a string
- extend - Merges the contents of two objects together
-
focusDisplayHandler - adds
data-focus-method
todocument.activeElement
which differentiates between keyboard, mouse and touch focus - focusTrap - trap keyboard tab focus within an element
- getCurrentMediaQuery - Returns the current media query in use in the CSS
- getIndex - Returns the index of a node in a nodeList
- getMetaContentByName - Returns a metatag content by name
-
getOffset - Get the current coordinates of a node, relative to the document (uses
getBoundingClientRect
) - getUrlParameterByName - Returns value of a parameter from a query string
- keyCodes - Abstraction of commonly used keycodes NB: object not a function
- ios100vhFix - stop iOS devices scaling up when rotating from portrait to landscape
- isBreakpoint - compare a breakpoint string against current CSS breakpoint
- jsonpRequest - Performs JSONP requests
- messages - Growl like messages
- objectifyForm - Generates a JS Object out of a form
- orientationChangeFix - Stopping iOS devices scaling up when rotating from portrait to landscape
- purgeProperties - Removes all properties from an object
- queryStringHandler
- toObject - Takes the passed URL, or the current browser URL and returns an object of query string parameters
- fromObject - Takes an object and turns it into a query string
- updateParameter - Updates a specified url parameter to a new value
- resized - What to do on document resize, also checks if current media query has changed
-
responsiveImageUpdate - force Safari to re-asses
srcset
on resize - scrollToY - Animated scroll to position
- sentEventToSegmentio - Tracks site wide requests for analytics events. Catches events before segment.io is ready, stores them and tries them again when it is ready. Follows conventions from segment.io: https://segment.com/docs/libraries/analytics.js/#selecting-integrations Checks a meta tag on init for identifying the user
- setFocusOnTarget - Sets focus on target node
- Store - Create your store with actions, mutations and an initial state
- fontLoadObserver - Adds a class and to the document and writes a cookie when fonts have loaded
- forEach - run a function on each item of an array
- lazyLoad - Lazy load images, videos, iframes, a A17-helperised version of: https://github.com/area17/lazyload
- manageBehaviors - inits and manages page data-behaviors on initial load and on page update
- triggerCustomEvent - Triggers a custom event
Feel free to add any vanilla JS helpers that will be common to many projects and save someone some time working them out in the future. The main aims are no dependencies, small elegant code and high potential sharing across sites.
Download the editor config plugin for your text editor: http://editorconfig.org/#download
Intentionally vanilla JS to keep this JS library agnostic.
Written using Mochajs and Chai.js, currently living in test/test.js
. To run:
$ npm run compile
$ npm run test
- Make your changes
- Bump the version number
- Test it, you may need to update the tests in /test/
- Update the wiki document for it if required
- Tell @fe in the developers Hipchat room
- Have an 🍦