WeCodePixels / theia-sticky-sidebar

Glues your website's sidebars, making them permanently visible while scrolling.
MIT License
569 stars 209 forks source link

More config params + only append style tag once #41

Closed andrewchilds closed 7 years ago

andrewchilds commented 7 years ago

Hello! This PR adds three things:

  1. Adds a defaultPosition option (useful if the bound element uses something other than position: relative by default).
  2. Adds a namespace option, to make it possible to unbind elements (this is useful if we want to re-render bound elements). This fixes issue https://github.com/WeCodePixels/theia-sticky-sidebar/issues/31.
  3. Only append the style tag to the HEAD if it doesn't already exist. Otherwise we end up with a duplicate style tag for every time this library is initialized.

Let me know if you need anything further to get this merged.

Cheers! Andrew

andrewchilds commented 7 years ago

And just for reference, to unbind bound elements, we do something similar to...

var LIBRARY_NAMESPACE = 'TSS';
var LIBRARY_CLASSNAME = '.theiaStickySidebar';

function destroy(boundElement) {
  // Unbind scroll / resize event handlers:
  $(window).off('resize.' + LIBRARY_NAMESPACE);
  $(document).off('scroll.' + LIBRARY_NAMESPACE);

  // Remove style tag:
  $('#theia-sticky-sidebar-stylesheet-' + LIBRARY_NAMESPACE).remove();

  // Remove wrapper element:
  boundElement = $(boundElement);
  boundElement.removeAttr('style').html(boundElement.find(LIBRARY_CLASSNAME).html());
}

function destroyAll() {
  $(LIBRARY_CLASSNAME).parent().each(function (i, element) {
    exports.destroy(element);
  });
};
liviucmg commented 7 years ago

Thank you @andrewchilds and @webfella, very good points all around. 🍻 Not sure if the destroy logic should be built-in as well, but it's a great start. I'll merge this, re-compile the dist files, and finally bump the version number.

andrewchilds commented 7 years ago

Great, thanks @liviucmg! The only thing remaining I think is for you to push the v1.7.0 tag to github and npm publish.

Cheers, Andrew

liviucmg commented 7 years ago

Good catch @andrewchilds, all done now. Thanks!

conanliuhuan commented 5 years ago

thanx