Skip to content

πŸ•΅οΈβ€β™€οΈ Select using a DOM ID not just an element

Latest
Compare
Choose a tag to compare
@rowanc1 rowanc1 released this 23 Nov 23:01
· 3 commits to main since this release

PR: #10
Commits:

  • πŸ•΅οΈβ€β™€οΈ Select using a DOM ID not just an element
  • πŸ“ Toggle disableNextDeselectSidenote
  • 1️⃣ Only deselect if necessary
  • πŸ“– Update readme with new functionality
  • 🍁 Fallback to other anchors if element is not defined

Actions

It is common to put a click handler on the body (or similar) to deselect any sidenotes. This can be difficult to stop in some cases, but can be anticipated with a onClickCapture that fires the
disableNextDeselectSidenote action. This intercepts the redux action and stops it from happening for one time.

Simple Javascript

You can also use sidenotes from vanilla javascript, this is done by first connecting the ID.

// First dispatch the action to connect to any ID in the dom
store.dispatch(actions.connectAnchor(docId, sidenoteId, anchorId));

// Then setup your handlers to select that anchor on click
<span
  id={anchorId}
  onClickCapture={(event) => {
    event.stopPropagation();
    store.dispatch(actions.selectAnchor(docId, anchorId));
  }}
>
  Select a Sidenote with JavaScript! πŸš€
</span>;

// To clean up later, disconnect the anchor
store.dispatch(actions.disconnectAnchor(docId, anchorId));