Modaal icon indicating copy to clipboard operation
Modaal copied to clipboard

Remove jQuery dependency

Open medikoo opened this issue 8 years ago • 9 comments

Why this needs jQuery? Why do I need to load full jQuery lib just be able to use it? It doesn't seem justifiable in 2016

medikoo avatar Apr 25 '16 07:04 medikoo

Hi @medikoo Thanks for opening the conversation, it's certainly something we've thought about. At this time however, this is the library we chose to build on top of. We agree that there is value in not having a dependency and we will consider it down the line for expansion of the plugin.

danhumaan avatar Apr 26 '16 00:04 danhumaan

We're talking now of maybe doing this sooner rather than later. Will report back later this week with thoughts.

cbotman avatar Apr 26 '16 10:04 cbotman

Moving these examples here from #29 since I closed it.

e.g. var dom = document.body; var element = document.querySelector('.class-name|#id'); if(element.classList.contains('class-name') {}; element.addEventListener('click|keydown', function); etc.

bklik avatar May 02 '16 13:05 bklik

Yeah at first glance I thought this would be straight forward too, but we are using jQuery for more than just fancy DOM selectors and things. Yesterday I got as far as compiling a list of all the jQuery methods we use, so can look at what it would take to replace them all.

As you can see there are quite a lot of calls. So the question is where do we draw the line between implementing a lot of basic methods (adding weight to Modaal even when jQuery is present), versus depending on a library?

I would like to take a look at other UI/widget libraries (things like Isotope or even other modals), and see how they've handled this.

For replacing animate(), I can imagine having a sort of progressive enhancement. So if you have included a library that provides the animate API, such as jQuery or Velocity.js, we use it, otherwise we skip the animation and jump to the end result. E.g. an element might show or hide instantly rather than fading. Just an idea.

I'm sure we're not the first to have this discussion, so please feel free to share anything you know of that's relevant. :)

The list:

Handy selectors and DOM traversal: $() element.children() element.contains() element.find() element.is()

DOM manipulation: element.addClass() element.append() element.attr() and element.getAttribute() (why both?) element.clone() element.css() element.data() element.empty() element.hasClass() element.hide() element.html() element.on() - bind event element.off() - unbind event element.removeAttr() element.removeClass()

Animation: element.animate() - only used to fade opacity and call oncomplete() callback element.stop()

AJAX: $.ajax() (.success(), .error() and .abort() callbacks)

Misc: array.extend() (options array, etc)

cbotman avatar May 03 '16 03:05 cbotman

You could use Zepto, which has the same API, but much smaller file size. If you'd like to go vanilla JS (I recommend, because jQuery is an unnecessary abstraction today), You Might Not Need jQuery is a good resource. Here is some stuff I found as a response to your list:

Selectors and DOM traversal

$()

The dollar does a lot of things. If you're using it for selecting, use these:

  • document.querySelector(selector)
  • document.querySelectorAll(selector)

$el.children()

Vanilla: el.children

However, note that document.querySelectorAll returns a NodeList, .children returns a HTMLCollection etc. None of these can be looped through directly because they are not arrays, they are array-like. You can transform them into arrays using Array.from().

$el.contains($child)

Vanilla: el !== child && el.contains(child)

$el.find(selector)

Vanilla: el.querySelector(selector)/el.querySelectorAll(selector)

$el.is($otherEl)

Vanilla: el === otherEl

DOM manipulation

$el.addClass()/$el.removeClass()

For all classy stuff use classie for IE8+ or these for IE10+:

  • el.classList.add()
  • el.classList.remove()

$el.append()

Vanilla: el.appendChild()

$el.attr()/$el.removeAttribute()

Vanilla:

  • el.getAttribute(attr)
  • el.setAttribute(attr, value)
  • el.removeAttribute()

$el.clone()

Vanilla: el.cloneNode(true)

$el.css('background', 'blue')

Vanilla: el.style.background = 'blue'

$el.data()

Vanilla: ?

$el.empty()

Vanilla: el.innerHTML = ''

$el.hide()

Vanilla: el.style.display = 'none'

$el.html()

Vanilla: el.innerHTML

$el.on()/$el.off()

For all eventy stuff use eventie for IE8+ or these for IE9+:

  • el.addEventListener()
  • el.removeEventListener()

AJAX

Maybe it's a good idea to use the Fetch API if IE10+ is acceptable. Otherwise you could find a tiny AJAX library on Microjs.

silvenon avatar May 10 '16 23:05 silvenon

Hi @silvenon, thanks for putting that together. I'll have a good read and try to get some time on this late next week.

cbotman avatar May 13 '16 08:05 cbotman

Any progress on this?

faradaytrs avatar Jun 12 '19 02:06 faradaytrs

What's the status on this? Or is this project dead?

kareljan avatar Mar 26 '20 15:03 kareljan

hi @kareljan - project is technically not dead, this github may seem very quite but it is a tool we still use nearly daily. The difficulty has been dedicating time to it and work through both updates, and bugs as other, billable, projects unfortunately take some priority.

This particular task is something that is definitely on our minds with the plugin, though I can't provide any more detail than that, ora timeframe on when it may be looked at.

Thanks for your patience.

danhumaan avatar Mar 26 '20 23:03 danhumaan