Skip to content

Commit c73ec5f

Browse files
authored
Sticky menubar based on JS observer (#189)
Applies fixed positioning to menu bar when the above switch button moves out of the viewport
1 parent d291c55 commit c73ec5f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

script.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,34 @@ function handleEditSession() {
167167
$toggleEditorButton.on('click', toggleEditor);
168168
}
169169

170+
/**
171+
* when the editor switch button moves out of the view-port, the menubar gets a class
172+
* @see https://codepen.io/hey-nick/pen/mLpmMV
173+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
174+
*/
175+
176+
function menubar() {
177+
const editorswitch = document.querySelector('button[name=prosemirror]');
178+
const menubar = document.querySelector('#prosemirror__editor div.menubar');
179+
const observer = new IntersectionObserver(
180+
([e]) => {
181+
return menubar.classList.toggle('prosemirror-menubar-fixed', e.intersectionRatio !== 1);
182+
},
183+
{
184+
root: null,
185+
threshold: [0, 1]
186+
}
187+
);
188+
observer.observe(editorswitch);
189+
}
190+
191+
170192
jQuery(function () {
171193
initializeProsemirror();
172194
window.proseMirrorIsActive = false;
173195

196+
jQuery(menubar);
197+
174198
if (jQuery('#dw__editform').find('[name=prosemirror_json]').length) {
175199
handleEditSession();
176200
}

style.less

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,16 @@
8585
display: block;
8686

8787
.menubar {
88-
position: sticky;
89-
top: 0; // Users/Theme developers can override this to stick it below a sticky header
9088
margin-bottom: 0.5rem;
9189
border: @border-style;
9290
border-radius: @border-radius;
91+
92+
&.prosemirror-menubar-fixed {
93+
position: fixed;
94+
top: 0;
95+
border: 3px solid @ini_border;
96+
padding: 1em;
97+
}
9398
}
9499
}
95100

0 commit comments

Comments
 (0)