• Stars
    star
    199
  • Rank 196,105 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

One kilobyte library for easy DOM manipulation

CodeQL Coverage Status Socket Badge CodeFactor Quality Gate Status Size npm version GitHub

Carbonium

One kilobyte library for easy DOM manipulation

With carbonium, you can call $(selector) and the result can be accessed as both an DOM element and an array of matched elements. DOM element operations are applied to all matched elements.

Examples

To set the left CSS property of all elements with the class indent to 40 pixels:

$(".indent").style.left = "40px";

To add the class important to all div's with "priority" as content:

$("div")
  .filter((el) => el.textContent == "priority")
  .classList.add("important");

You can use carbonium to create elements:

const error = $("<div class='error'>An error has occured!</div>")[0];

Installation

npm install --save-dev carbonium

Now you can import carbonium:

import { $ } from "carbonium";

If you don't want to install or use a bundler like webpack or rollup.js, you can import carbonium like this:

const { $ } = await import(
  "https://cdn.jsdelivr.net/npm/carbonium@1/dist/bundle.min.js"
);

API

Select elements

$(selector [, parentNode])

Parameters

Name Type Description
selector string Selector to select elements
parentNode Document | Element (optional) Document or element in which to apply the selector, defaults to document

Returns

An array of the matched elements, which can also be accessed as a single element.

Create element

$(html [, parentNode])

Parameters

Name Type Description
html string HTML of element to create, starting with "<"
parentNode Document | Element (optional) Document or element in which to apply the selector, defaults to document

Returns

An array with one created element.

TypeScript

If you use TypeScript, it's good to know Carbonium is written in TypeScript and provides all typings. You can use generics to declare a specific type of element, for example HTMLInputElement to make the disabled property available:

$<HTMLInputElement>("input, select, button").disabled = true;

Why?

You might find most frameworks are quite bulky and bad for performance (1). On the other side, you might find using native DOM and writing document.querySelectorAll(selector) each time you want to do some DOM operations to become tedious. You can write your own helper function, but that only takes part of the pain away.

Carbonium seeks to find the sweet spot between using a framework and using the native DOM.

jQuery

Isn't this just jQuery and isn't that obsolete and bad practice?

No. Carbonium doesn't have the disadvantages of jQuery:

  1. Carbonium is very small: just around one kilobyte.
  2. There's no new API to learn, carbonium provides only standard DOM API's.

Browser support

Carbonium is supported by all modern browsers. It is tested to work on desktop and mobile with Firefox 79, Chrome 84, Safari 13 and Edge 84. It should work with all browsers supporting Proxy, see Can I use Proxy for support tables.

Name

Photo of diamond and graphite

Carbonium is the Latin name for carbon. Carbon has two forms (allotropes): graphite and diamond. Just like this library, in which the result presents itself both as one element and as a list of elements.

Photo CC BY-SA 3.0

License

Copyright 2023 Edwin Martin and released under the MIT license.

More Repositories

1

Color-animation-jQuery-plugin

Color animation jQuery-plugin
JavaScript
27
star
2

WebUSB

WebUSB enabled LOL shield
JavaScript
26
star
3

miq

Super lightweight jQuery-like DOM library using modern JavaScript features like property descriptors and ES5 array functions
JavaScript
22
star
4

Game-of-Life-in-Java

John Conways Game of Life in Java, either as a applet or as a stand alone program.
Java
19
star
5

Beautify

Powerful article formatting with MarkDown, SmartyPants, GeSHi and Dot combined
PHP
17
star
6

game-of-life

Play the Game of Life in your browser
HTML
15
star
7

testProxy

Test your locally running websites on other (mobile) devices
JavaScript
11
star
8

memoize-cache-decorator

Add @memoize() to your class methods to have the result cached for future method calls.
TypeScript
10
star
9

Shadow-animation-jQuery-plugin

Shadow animation jQuery plugin
JavaScript
9
star
10

mClass

Provides class like programming including private variables to JavaScript while staying close to prototypal inheritance.
JavaScript
5
star
11

TinyMatrix

Files used during my TinyMatrix microcontroller with LED matrix workshop
C
4
star
12

web-component-decorator

Lightweight TypeScript decorators for web components for easier handling of attribute changes and cleaner code
TypeScript
4
star
13

Jarx

Runs the JVM from an executable. Use it to make running your Java programs on Windows easy.
C
4
star
14

ipconfig

ipconfig HTA wrapper
3
star
15

csp-report-php

Receive CSP reports in your Slack
PHP
1
star
16

rxjslog

Log RxJS events nicely to the console
JavaScript
1
star
17

snippets

Miscellaneous code snippets for frontend web developers
1
star
18

range

Create an array of numbers within a given range.
JavaScript
1
star
19

SPR_EDIT

Sprite editor for the Atari ST written in GFA Basic
1
star
20

kilomega

Get the right unit prefix like kilo of mega for a certain bit or byte amount.
JavaScript
1
star
21

Tweak-Network-Firefox-Extension

Tweak Network Firefox Extension
JavaScript
1
star
22

Arduino-DCF77-oled-clock

My first Arduino project
Arduino
1
star
23

View-Cookies-Firefox-Extension

View Cookies Firefox Extension
JavaScript
1
star
24

tingle

Tiny WebGL library
JavaScript
1
star
25

micropython-vfd-driver

Micropython driver for the 8-MD-06INKM Futaba VFD display with 16 segments
Python
1
star