GithubHelp home page GithubHelp logo

appzic / elementid Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 2.0 294 KB

Smart way to manage IDs for frontend Javascript and Typescript projects.

License: MIT License

JavaScript 22.25% TypeScript 72.05% Dockerfile 1.06% Shell 4.64%
javascript nanoid typescript id-management cli command-line command-line-tool watch devdependency toml zero-dependency frontend frontend-web

elementid's Introduction

elementid-logo

ElementID

Smart way to manage IDs for frontend Javascript and Typescript projects.

GitHub Workflow Status npm npm GitHub

Table of Contents

Introduction

ElementID is an ID management tool for your frontend Nodejs (Javascript or Typescript) projects. You can share ID values between different modules using ElementID without any conflict. ElementID is a dev dependency. It provides unique or custom values to your production build according to the input IDs. ElementID has a caching system that helps to make ID values static.

Features

  • โš”๏ธ No ID conflicts
  • ๐ŸŽ‰ Zero dependencies in production
  • ๐Ÿ“ˆ Increase your productivity
  • ๐Ÿ’ช Generate unique IDs with the powerful nanoid generator
  • ๐Ÿ‘€ Auto generate with watch mode
  • ๐Ÿ’ฝ Cache static unique values
  • ๐Ÿ’ป Powerful CLI tool
  • ๐Ÿ“œ Simple input configuration with .toml

Getting Started

Installation

We recommend installing ElementID as a dev dependency

npm i -D elementid

Create TOML file

Declare your project IDs in .toml format. We recommend the Even Better TOML extension for VS Code users to create the .toml file.

The ElementID can generate two types of ID values.

  • Unique values - To generate a unique value for your declared ID, set the value in your toml file to an empty string ("").
yellowBtnId = ""
redBtnId = ""
  • Custom values - Provide any custom ID values as usual.
greenBtnId = "my_custom_value_1"
blueBtnId = "my_custom_value_2"

Generate IDs

The ElementID has a powerful CLI tool for generating IDs according to your input. If your input file path is ./my_ids.toml, you can use the following command to generate IDs.

elementid ./my_ids.toml

Please read the command line section for more CLI options.

Implement IDs

The ElementID generates Javascript(.js) and type declaration(.d.ts) files in the node_modeules/elementid/dist/ directory.

Usage example:

// ES6 syntax
import { yellowBtnId, redBtnId, greenBtnId } from "elementid";

const yellowBtn = document.getElementById(yellowBtnId);
const redBtn = document.querySelector(`#${greenBtnId}`);
// CommonJS syntax
const ids = require("elementid");

const yellowBtn = document.getElementById(ids.blueBtnId);
const redBtn = document.querySelector(`#${ids.redBtnId}`);

Command-line

Usage: elementid <input file> [options]

Options:
  -w, --watch       Watch changes of input file                            [boolean]
  -f, --force       Generate unique IDs without caching                   [boolean]
  -l, --length      Specify length of generated unique IDs
                    (default = 8, options = 5, 6, 7, 8, 9, 10)              [number]
  --version         Show version number                                    [boolean]
  -h, --help        Show help                                              [boolean]

Examples:
  elementid ids.js                      Default usage without additional options
  elementid ids.js --length=7           Sets the length of generated IDs to 7
  elementid src/my_ids.js --watch       Uses the watch option

Use Cases

Astro

Astro is a static site generator. Astro has its own component to build a static site. There are four sections to generate HTML, CSS, and Javascript. Read more about Astro components.

  • HTML content helper
  • HTML content
  • Scripts
  • Styles

Making a dynamic web page with Astro requires access to the DOM elements. The function getElementById() is a popular method to get access to the DOM elements from Javascript. However, Astro does not allow the same ID to be used in both the HTML content and Script sections.

While hardcoding ID values is an option, this strategy presents an increased opportunity for developer error and conflicts.

ElementID helps you to solve this problem. You can access the same ID values with ElementID in the HTML content helper section and Script sections. Check out the following example for how to use ElementID in Astro.

---
import { myBtnId } from "elementid";
---

<div>
    <button id={myBtnId}>Click ME!</button>
</div>

<script>
    import { myBtnId } from "elementid";

    myBtn = document.getElementById(myBtnId);
    myBtn.addeventlistener("click",()=>{
        console.log("click my button");
    });
</script>

Contributing

If you want to open an issue, create a Pull Request, or simply want to know how you can run it on your local machine, please read the Contributing Guide.

Contributors

License

ElementID is MIT licensed.

elementid's People

Contributors

cerrussell avatar y-developer avatar

Stargazers

 avatar

elementid's Issues

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.