Git Product home page Git Product logo

choozy's Introduction

choozy

choozy is a tiny 331 byte DOM utility that simplifies the selection of multiple elements

Installation

npm i choozy

Usage

Given the following HTML

<html>
  <body>
    <header class="js-header">
      <h1 class="js-logo">Logo</h1>
      <nav class="js-nav">
        <button class="js-button">Menu</button>
        <ul class="js-list">
          <li class="js-items"></li>
          <li class="js-items"></li>
          <li class="js-items"></li>
        </ul>
      </nav>
    </header>
    <main class="js-main"></main>
    <footer class="js-footer"></main>
  </body>
</html>

Before choozy

const dom = {
  header: document.querySelector('.js-header'),
  logo: document.querySelector('.js-logo'),
  nav: document.querySelector('.js-nav'),
  button: document.querySelector('.js-button'),
  list: document.querySelector('.js-list'),
  items: [...document.querySelectorAll('.js-items')],
  main: document.querySelector('.js-main'),
  footer: document.querySelector('.js-footer'),
}

After choozy

If multiple elements contain the same suffix, they are collected into an array.

import choozy from 'choozy'

const dom = choozy()

Options

The first argument is the container that choozy will query. This is the body element by default.

The second argument is the prefix that will be used to select elements. By default, this is js-

choozy((container = document.body), (prefix = 'js-'))

Examples

In a single page app

Perhaps you're writing a single page application and you have a base view class that all of the views in your application extend. You could use choozy to handle dom selection so all you need to do is add classes to the elements you need:

import choozy from 'choozy'

class BaseView {
  constructor(container) {
    this.refs = choozy(container)
    this.init()
  }
}

class Home extends BaseView {
  constructor(container) {
    super(container)
  }

  init() {
    // access your elements via `this.refs`
  }
}

Change the prefix for your entire app

In order to keep this utility as tiny as possible, the choozy function takes 2 arguments instead of an options object. So if you wanted to change the prefix to foo- for your whole app, you could do something like this:

/* util.js */

import choozy from 'choozy'

const myChoozy = container => choozy(container, 'foo-')

export default myChoozy

Inspiration

This package is inspired by query-dom-components

License

MIT License ยฉ Mike Wagz

choozy's People

Contributors

estrattonbailey avatar mikehwagz avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.