Skip to content

Commit

Permalink
Sticky menubar based on JS observer (#189)
Browse files Browse the repository at this point in the history
Applies fixed positioning to menu bar when the above switch button moves out of the viewport
  • Loading branch information
annda authored Nov 1, 2023
1 parent d291c55 commit c73ec5f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 24 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,34 @@ function handleEditSession() {
$toggleEditorButton.on('click', toggleEditor);
}

/**
* when the editor switch button moves out of the view-port, the menubar gets a class
* @see https://codepen.io/hey-nick/pen/mLpmMV
* @see https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
*/

function menubar() {
const editorswitch = document.querySelector('button[name=prosemirror]');
const menubar = document.querySelector('#prosemirror__editor div.menubar');
const observer = new IntersectionObserver(
([e]) => {
return menubar.classList.toggle('prosemirror-menubar-fixed', e.intersectionRatio !== 1);
},
{
root: null,
threshold: [0, 1]
}
);
observer.observe(editorswitch);
}


jQuery(function () {
initializeProsemirror();
window.proseMirrorIsActive = false;

jQuery(menubar);

if (jQuery('#dw__editform').find('[name=prosemirror_json]').length) {
handleEditSession();
}
Expand Down
9 changes: 7 additions & 2 deletions style.less
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@
display: block;

.menubar {
position: sticky;
top: 0; // Users/Theme developers can override this to stick it below a sticky header
margin-bottom: 0.5rem;
border: @border-style;
border-radius: @border-radius;

&.prosemirror-menubar-fixed {
position: fixed;
top: 0;
border: 3px solid @ini_border;
padding: 1em;
}
}
}

Expand Down

0 comments on commit c73ec5f

Please sign in to comment.