🗿 Surreal - Hyper minimalist jQuery alternative

Overview

🗿 Surreal - Hyper minimalist jQuery alternative

cover (Art by shahabalizadeh)

CI build badge Mini build badge License badge

💙 Why does this exist?

For devs who love ergonomics!

If you agree with any of the following, you may appreciate Surreal:

  • Love staying as close to Vanilla JS.
  • Hate typing document.querySelector over.. and over..
  • Hate typing addEventListener over.. and over..
  • Really wish document.querySelectorAll had Array functions..
  • Really wish this would work in child ">
    <label for="file-input" >
      <div class="uploader">div>
      <script>
        me().on("dragover", ev => { halt(ev); me(ev).classAdd('.hover'); console.log("Files in drop zone.") })
        me().on("dragleave", ev => { halt(ev); me(ev).classAdd('.hover'); console.log("Files left drop zone.") })
        me().on("drop", ev => { halt(ev); me(ev).classRemove('.hover').classAdd('.loading'); me('#file-input').attribute('files', ev.dataTransfer.files); me('#form').trigger('change') })
      script>
    label>

👁️ Live Example

Get a taste- see the Showcase! Then view source.

🎁 Installation

Surreal is a dependency-free, browser-oriented javascript library. Download Surreal and drag surreal.js into the appropriate directory of your project. Using it is as simple as adding a ">

<script src="surreal.js">script>

🤔 Why choose me() and any() over $ and $$

  • No ambiguity unlike jQuery's approach of $ for both single elements and Arrays. Less need for sanity checks.
  • Readability. English-like and self documenting.
  • Same verbbage as Hyperscript making this an excellent transitional library.
  • It can be far less wordy to work with elements directly instead of an Array.

📚️ Inspired by

  • jQuery for the chainable syntax we all love.
  • BlingBling.js for modern minimalism.
  • Bliss.js for a focus on single elements and extensibility.
  • Hyperscript for Locality of Behavior and awesome ergonomics.
  • Shout out to Umbrella, Cash, Zepto- Not quite as ergonomic or extensible.

🔥 Usage Overview

🔍️ DOM Selection

You want one element directly.

  • me(...)
    • me() Get the current element.
      • Provides Locality of Behavior in
">
<div>I change color every second.
  <script>
    // Locality of Behavior
    me().on("click", async ev => {
      me(ev).styles({ "transition": "background 1s" })
      await sleep(1000)
      me(ev).styles({ "background": "red" })
      await sleep(1000)
      me(ev).styles({ "background": "green" })
      await sleep(1000)
      me(ev).styles({ "background": "blue" })
      await sleep(1000)
      me(ev).styles({ "background": "none" })
      await sleep(1000)
      me(ev).remove()
    })
  script>
div>
{ me(ev).fadeOut() })
">
<div>I fade out and remove myself.
  <script>
    // Keepin it simple! Locality of Behavior.
    me().on("click", ev => { me(ev).fadeOut() })
  script>
div>
">
<div>I change color every second.
<script>
  // Run on load.
  (async (el = me())=>{
    me(el).styles({ "transition": "background 1s" })
    await sleep(1000)
    me(el).styles({ "background": "red" })
    await sleep(1000)
    me(el).styles({ "background": "green" })
    await sleep(1000)
    me(el).styles({ "background": "blue" })
    await sleep(1000)
    me(el).styles({ "background": "none" })
    await sleep(1000)
    me(el).remove()
  })()
script>
div>
">
<script>
  // Keepin it simple! Globally!
  (async ()=>{
    any("button").fadeOut()
  })()
script>

Array methods

any('button')?.forEach(...)
any('button')?.map(...)

👁️ Function Reference

Looking for DOM Selectors?

🧭 Legend

  • 🔗 Chainable off me() and any()
  • 🌐 Global convenience helper.
  • 🔥 Runnable example.
  • ❤️‍🔥 Alias.
  • 🔌 Built-in Plugin

👁️ At a glance

  • 🔗 run
    • 🔥 me().run(el => { alert(el) })
    • 🔥 any('button').run(el => { alert(el) })
    • It's forEach but less wordy and works on single elements.
  • 🔗 remove
    • 🔥 me().remove()
    • 🔥 any('button').remove()
  • 🔗 classAdd ❤️‍🔥 class_add
    • 🔥 me().classAdd('active')
    • Leading . is optional for all class functions, to prevent typical syntax errors with me() and any().
      • me().classAdd('active') and me().classAdd('.active') are equivalent.
  • 🔗 classRemove ❤️‍🔥 class_remove
    • 🔥 me().classRemove('active')
  • 🔗 classToggle ❤️‍🔥 class_toggle
    • 🔥 me().classToggle('active')
  • 🔗 styles
    • 🔥 me().styles('color: red') Add style.
    • 🔥 me().styles({ 'color':'red', 'background':'blue' }) Add multiple styles.
    • 🔥 me().styles({ 'background':null }) Remove style.
  • 🔗 attribute ❤️‍🔥 attributes ❤️‍🔥 attr
    • Get: 🔥 me().attribute('data-x')
      • Get is only for single elements. For many, wrap the call in any(...).run(...) or any(...).forEach(...).
    • Set: 🔥 me().attribute('data-x', true)
    • Set multiple: 🔥 me().attribute({ 'data-x':'yes', 'data-y':'no' })
    • Remove: 🔥 me().attribute('data-x', null)
    • Remove multiple: 🔥 me().attribute({ 'data-x': null, 'data-y':null })
  • 🔗 trigger
    • 🔥 me().trigger('hello')
    • Wraps dispatchEvent
  • 🔗 on
    • 🔥 me().on('click', ev => { me(ev).styles('background', 'red') })
    • Wraps addEventListener
  • 🔗 off
    • 🔥 me().remove('click')
    • Wraps removeEventListener
  • 🔗 offAll
    • 🔥 me().offAll()
  • 🌐 sleep
    • 🔥 await sleep(1000, ev => { alert(ev) })
    • async version of setTimeout
    • Wonderful for animation timelines.
  • 🌐 tick
    • 🔥 await tick()
    • await version of rAF / requestAnimationFrame.
    • Animation tick. Waits 1 frame.
    • Great if you need to wait for events to propagate.
  • 🌐 rAF
    • 🔥 rAF(e => { return e })
    • Animation tick. Fires when 1 frame has passed. Alias of requestAnimationFrame
    • Great if you need to wait for events to propagate.
  • 🌐 rIC
    • 🔥 rIC(e => { return e })
    • Great time to compute. Fires function when JS is idle. Alias of requestIdleCallback
  • 🌐 halt
    • 🔥 halt(event)
    • Great to prevent default browser behavior: such as displaying an image vs letting JS handle it.
    • Wrapper for preventDefault
  • 🌐 createElement ❤️‍🔥 create_element
    • 🔥 el_new = createElement("div"); me().prepend(el_new)
    • Alias of document.createElement
  • 🌐 onloadAdd ❤️‍🔥 onload_add
    • 🔥 onloadAdd(_ => { alert("loaded!"); })
    • Execute after the DOM is ready. Similar to jquery ready()
    • Queues functions onto window.onload
    • Why? So you don't overwrite window.onload, also predictable sequential loading!

🔌 Built-in Plugins

Effects

You can build your own effects easily with me().styles({...}) then timelining CSS transitions using await or callbacks, but we ship the most common effects for ergonomics:

  • 🔗 fadeOut ❤️‍🔥 fade_out
    • Fade out and remove element.
    • 🔥 me().fadeOut()
    • 🔥 me().fadeOut(ev => { dosomething() }, 3000) Over 3 seconds then call function.

🔮 No Surreal Needed

Some patterns are already as short as you can get in vanilla JS!

Logging

  • 🌐 console.log() console.warn() console.error()
  • Event logging: 🔥 ev.monitorEvents(me()) See: Chrome Blog

Children

  • 🔥 me().children
  • 🔥 me().children.hidden = true

Append / Prepend elements.

  • 🔥 me().prepend(el_new)
  • 🔥 me().appendChild(el_new)
  • 🔥 me().insertBefore(el_new, el.firstChild)
  • 🔥 me().insertAdjacentHTML("beforebegin", el_new)

Text / HTML Content

  • me().innerHTML = "

    hello world

    "
  • me().innerText = "hello world"

💎 Conventions & Tips

  • _ = are fine for temporary or unused variables. Keep it short and sweet!
  • e, el, elt = element
  • e, ev, evt = event
  • f, fn = function
  • Developer ergonomics and simplicity wins.
  • Find the layer where the change needs to touch the least places.
  • Animations are done with me().styles(...) with CSS transitions. Use await sleep(...) for timelining.
  • Modals and dropdowns can be done in pure HTML / CSS now.

🔌 Extending Surreal

First off, you can certainly just add to your Surreal core. Surreal is designed to be small, auditable and understandable. But we also have a plugin system for less core-like features if you prefer:

  1. Add your function to Surreal
var $thing = {
  test(e, name) {
    console.log(`Hello ${name} from ${e}`)
    return e
  }
}
$ = {...$, ...$thing}
  1. Add your function to Surreal sugar() if it is chainable.
$.sugars['test'] = (name) => { return $.test($._e, name) }
  1. Automatically will be added as a global convenience with globalsAdd() If this should not be allowed, please add it to the restricted list in globalsAdd()

If applicable, make your function compatible with both single elements and arrays. Refer to an existing function to see how.

Make an issue or pull request if you think people would like to use it! If it's useful enough we may want it in the core!

🌘 Future

You might also like...

This is a To-Do List. It shows a minimalist design with the next features: Add new tasks, edit tasks, markup completed tasks, and erase all completed tasks. Built with JavaScript.

Project Name To Do List Built With HTML CSS JavaScript Live Demo To do List Live Demo Link Getting Started This is a To Do List. It shows a minimalist

Jun 9, 2022

A minimalist to-do website bootstrapped with webpack.

To-Do App A minimalist to-do app bundled with webpack. Built With Javascript, CSS, HTML Live Demo (if available) Live Demo Link Authors 👤 Author1 Git

Dec 7, 2022

A (very) minimalist creative coding playground. Make animations using only 64 HTML sliders!

Sliderland A (very) minimalist creative coding playground. Make animations using only 64 HTML sliders! Credits The recording feature uses ffmpeg.wasm

Dec 30, 2022

A minimalist Javascript library to perform AJAX POST and GET Request.

minAjax.js A minimalist Javascript library to perform AJAX POST and GET Request. #Check Pretty Documentation http://flouthoc.github.io/minAjax.js/ #Us

Apr 27, 2021

Keep track of your tasks with this minimalist to do list made with Javascript CSS HTML and Webpack.

Keep track of your tasks with this minimalist to do list made with Javascript CSS HTML and Webpack.

my-to-do-list- keep track of your tasks with this to do list DESCRIPTION build a simple HTML list of To Do tasks. LIVE DEMO "https://andgarzonmal.gith

Jul 21, 2022

⚡️ A fast, minimalist web framework for the Bun JavaScript runtime

🥟 Bao.js A fast, minimalist web framework for the Bun JavaScript runtime. ⚡️ Bao.js is 3.7x faster than Express.js and has similar syntax for an easy

Dec 26, 2022

🤍 A minimalist portfolio template for Developers

🤍 A minimalist portfolio template for Developers

ForDaFolio 🚀 A minimal portfolio template for Developers! Note: Since this template is currently being worked on, expect that the code is not yet org

Oct 17, 2022

A Minimalist to do list website where user can add, remove and edit multiple tasks and All the changes user makes in his to do list is saved in browser local storage so that it can be accessed later.

Testing for Add Remove function in To Do List App Jest framework is used for testing. Created (addremove.test.js) for a file containing the add item a

Aug 15, 2022

Story Show Gallery - JS & React - minimalist, vertical photo gallery, mobile friendly

 Story Show Gallery - JS & React - minimalist, vertical photo gallery, mobile friendly

Vertical photo gallery optimized for smartphones (notch area included). SSG will support your brand and marketing. Optimally placed captions, full screen lightbox, no ugly arrows

Oct 4, 2022
Comments
  • fadeIn / fade_in effect as convenience built-in.

    fadeIn / fade_in effect as convenience built-in.

    The opposite compliment of fadeOut / fade_out: https://github.com/gnat/surreal/blob/e8b3ae5683dfdc3487a6d99cefa0fe25a8674117/surreal.js#L249

    fadeIn should animate into existince an element of opacity: 0 and accept a callback and time similar to the existing fadeOut.

    good first issue 
    opened by gnat 0
Releases(0.0.7)
Owner
Nathaniel Sabanski
Full Stack Software Engineer
Nathaniel Sabanski
A soothing pastel theme for Hyper.

Catppuccin for Hyper ~ Hypurr Contents Install License Install Please note that the name of NPM package and the theme itself is hypurr and not hyper-c

ashish 12 Dec 23, 2022
Zepto.js is a minimalist JavaScript library for modern browsers, with a jQuery-compatible API

Zepto.js – a minimalist JavaScript library Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you u

Thomas Fuchs 15k Dec 31, 2022
🍐 The modern jQuery alternative

?? Nashi ?? Work in progress The modern jQuery alternative. Motivation Write this: nashi('p').text('hello').addClass('foo').toggleClass('bar'); Not th

null 61 Dec 28, 2022
A minimalist web app for the daily morning and night athkar.

Morning & Night Athkar | أذكار الصباح والمساء Local Development Recommended: Use nvm to manage npm dependencies and versions. This app uses node versi

Mohammed Amarnah 4 Dec 2, 2022
Sharerbox - Free, minimalist and lightweight JavaScript-based social-media sharer for websites

Sharerbox Free minimalist and lightweight JavaScript-based social-media sharer for websites. Version: 0.8.1 Description SharerBox is a free, minimalis

Juan Astudillo 3 Aug 22, 2022
minimalist virtual dom library

petit-dom A minimalist virtual DOM library. Supports HTML & SVG elements. Supports Render functions and Fragments. Custom components allows to build y

Yassine Elouafi 485 Dec 12, 2022
This is a basic website Todo Application that displays a list that looks and behaves like the part of minimalist project.

To-Do-list-microverse Description This is a basic website; a Todo Application that displays a list that looks and behaves like the part of minimalist

Dennis Akagha 7 Feb 3, 2022
Minimalist Web XR Location Based Markers for A-Frame 1.3.0

LBAR.js “I understand how the engines work now. It came to me in a dream. The engines don't move the ship at all. The ship stays where it is and the e

Media Engineering Institute 9 Dec 3, 2022
A minimalist ToDo list app to add, update and remove tasks easily

To-do list is a tool that helps to organize your day. It simply lists the things that you need to do and allows you to mark them as complete. We will build a simple website that allows for doing that, and we will do it using ES6 and Webpack!

Oybek Kayumov 14 Jun 23, 2022
"To Do List" is a minimalist project that displays a list of task and allows you to add and remove task from that list. Built with JavaScript

To Do List Structure Solo programming project for module 2 week 2 of the Microverse Program. Live Demo Live Demo Link "To Do List" is a minimalist pro

Yersel Hurtado 7 Mar 5, 2022