Skip to content

Instantly share code, notes, and snippets.

@jmshal
Last active May 15, 2018 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmshal/4c7dd81a61adcb648944a16b0fac717f to your computer and use it in GitHub Desktop.
Save jmshal/4c7dd81a61adcb648944a16b0fac717f to your computer and use it in GitHub Desktop.
Electron `position: sticky` shim

electron-sticky-shim

This 8 line shim makes elements with position: sticky work as intended in situations where you have scrollable content nested within your app - see this for an example.

Installation

You can either copy the sticky-shim.js file from this gist, or install it as a module using npm...

$ npm install --save gist:4c7dd81a61adcb648944a16b0fac717f

How do I use it?

You will need to enable the position: sticky flag/blink feature in order for this to work.

You can do this by either adding CSSStickyPosition to the blinkFeatures web preference...

win = new BrowserWindow({
  // ...
  webPreferences: {
    blinkFeatures: 'CSSStickyPosition',
  },
});

Or you can enable all of the Chrome experimental features (of which position: sticky is one)...

win = new BrowserWindow({
  // ...
  webPreferences: {
    experimentalFeatures: true,
  },
});

Now all you need to do is import electron-sticky-shim (or manually include the sticky-shim.js file), and add the sticky-shim class to any elements which have the position: sticky css rule applied.

require('electron-sticky-shim');

// ...

The shim does not automatically add position: sticky to your sticky elements, this is so when Electron does support it natively, you can simply remove the shim and the shim class, and your CSS should work as intended.

<div class="container">
  <div class="sticky-shim" style="position: sticky; top: 0">
    Example
  </div>
  Lorem ipsum...
</div>

License

WTFPL - http://www.wtfpl.net/txt/copying

{
"name": "electron-sticky-shim",
"version": "0.0.0",
"main": "sticky-shim.js"
}
window.addEventListener('scroll', (event) => {
const stickyShim = event.target.querySelector('.sticky-shim');
if (stickyShim) {
stickyShim.style.position = 'relative';
stickyShim.offsetHeight;
stickyShim.style.position = '';
}
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment