Skip to content

Enhance sticky-margin functionality and improve documentation#9

Merged
Tom-van-Woudenberg merged 9 commits intomainfrom
trigger_and_figlxass
Apr 8, 2026
Merged

Enhance sticky-margin functionality and improve documentation#9
Tom-van-Woudenberg merged 9 commits intomainfrom
trigger_and_figlxass

Conversation

@douden
Copy link
Copy Markdown
Member

@douden douden commented Apr 1, 2026

Functionality added:

  • Sticky margin elements now work for images within figures, figures without images, images outside figures and directives.
  • Behaviour when scrolling can be set to full ("margin element appears when original element disappears above the header") or to partial ("margin element appears when original element reaches the header") and corresponding disappearing behavior.

douden and others added 5 commits April 1, 2026 13:06
Fix double show, scrollbar flickering, and sidebar trigger
Expand sticky-margin to work with images, figures and other HTML elements instead of only figure images. Update docs to refer to "elements" and show new examples (use :figclass for figure directives; :class still works for div-producing directives and image directives). Refactor JavaScript: rename source/target variables to sourceFlightElement/targetFlightElement, clone arbitrary elements (not just img), derive target from aside.querySelector('figure') || aside.firstElementChild, and update visibility/animation checks accordingly. Minor docs tweaks (added admonition examples and MathJax wording) and small code cleanup around preparation/visibility handling.
Replace the simple FIGURE/closest logic with explicit branches so DIV-tagged .sticky-margin elements are treated as standalone mainFigure. For other tags, fall back to the closest <figure> or the marker itself. This enables standalone or nested div.sticky-margin blocks to be sticky on their own and avoids missing mainFigure when no enclosing <figure> exists.
Change the include start marker in MANUAL.md from the visible heading '## Installation' to an explicit HTML comment '<!-- Install start -->'. Add this comment to README.md just before the Installation section so the include can target a stable, non-visible marker instead of matching visible heading text.
Introduce a configurable sticky_margin "trigger" (full or partial) and wire it from Sphinx config into the frontend. Adds app.add_config_value("sticky_margin", ...) and a config-inited handler that validates the value, logs a warning for invalid entries, and emits a small JS snippet setting stickyMarginTrigger. Update the sticky-margin.js to read and normalize the trigger, fallback to "full" for invalid values, and change visibility logic to support the "partial" mode and avoid unnecessary evaluations. Also update README with usage and config examples (including Jupyter Book) and add a logger import and docstring tweak.
@douden douden linked an issue Apr 1, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Member

@Tom-van-Woudenberg Tom-van-Woudenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

When applying a sticky-margin to this directive, I get a horizontal scrollbar (with trigger: full) during the flying animation and I don't see the actual animation:

::::{admonition} Uitwerking
:class: solution, dropdown, sticky-margin

In de $y$-richting kan de locatie gevonden worden ten opzichte van de rechter wand:

```{figure} ./toz_data/statisch_moment.svg
:align: center
:source: https://github.com/Structural-Mechanics-CEG/mechanics-figures-source/tree/main/TOZ_schuifspanningen
:number:
```

$$\bar y_{\rm{N.C.}} = \cfrac{S_{\bar y}}{A} = \cfrac{300 \cdot 12 \cdot \cfrac{300}{2}}{300 \cdot 12 + 300 \cdot 12} = 75 \, \rm{mm}$$

::::

Futhermore, the order in the sidebar is not correct anymore:

Image

The figure on the right goes partially out of view earlier than the other figure, so it should show first in the sidebar, now when the second figure goes partially out of view it moves the other figure down.

Clarify docs and guard against conflicting classes: update MANUAL.md to use "elements" instead of "figures" for sticky margin wording; extend README.md with a caution about DOM/render order and a note that combining the `sticky-margin` and `dropdown` classes is unsupported. Add runtime protection in sticky-margin.js to remove the `sticky-margin` class and emit a console warning when an element also has `dropdown`, preventing conflicting behaviors.
@douden
Copy link
Copy Markdown
Member Author

douden commented Apr 2, 2026

Looks good!

When applying a sticky-margin to this directive, I get a horizontal scrollbar (with trigger: full) during the flying animation and I don't see the actual animation:

::::{admonition} Uitwerking
:class: solution, dropdown, sticky-margin

In de $y$-richting kan de locatie gevonden worden ten opzichte van de rechter wand:

```{figure} ./toz_data/statisch_moment.svg
:align: center
:source: https://github.com/Structural-Mechanics-CEG/mechanics-figures-source/tree/main/TOZ_schuifspanningen
:number:

$$\bar y_{\rm{N.C.}} = \cfrac{S_{\bar y}}{A} = \cfrac{300 \cdot 12 \cdot \cfrac{300}{2}}{300 \cdot 12 + 300 \cdot 12} = 75 , \rm{mm}$$

::::

The scrollbar is a result of a too wide equation that cannot be truncated automatically. That is a restriction of MathJax.

The animation I did not get either, but without the class dropdown it did work as expected. In some cases the dropdown+sticky behavior causes breakdowns to happen, so I have added code that disables the sticky-margin class if the dropdown class is also present.

I also added this to the README.

Futhermore, the order in the sidebar is not correct anymore:

Image The figure on the right goes partially out of view earlier than the other figure, so it should show first in the sidebar, now when the second figure goes partially out of view it moves the other figure down.

In this case this behavior is as expected: when the full trigger is used, triggers are caused by the bottom of elements, in this case the captions, which align, so the order in which these elements will be added to the margin is then the DOM order, in this case from left to right. If the partial trigger is used, the triggers are caused by the top of elements, which in this case the top of the images do not align, so the first trigger is given by the right figure, which appears first in the margin. Then the left image appears in the margin, but because of the DOM order, it is inserted above the already present element. In some sense this also makes sense, as the numbering of the elements is based on the DOM/node order, and not on the visual order.

I have added a caution to the README to express this behavior.

Copy link
Copy Markdown
Member

@Tom-van-Woudenberg Tom-van-Woudenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the numbering of the elements is based on the DOM/node order

That's not the case. I made it such that the order is based on the order on the rendered page:
https://github.com/TeachBooks/Sphinx-Sticky-Margin/blob/main/src/sphinx_sticky_margin/assets/sticky-margin.js#L52

For 'partial' the priority of top and bottom should be swapped

Clarify ordering rules in README and update sorting logic to respect the sticky trigger mode. README now documents explicit sort orders for `full` (bottom, top, left, height, width, DOM) and `partial` (top, bottom, left, height, width, DOM). JavaScript sorting was adjusted to check `stickyTriggerMode`: in `full` mode figures are compared by bottom then top, while in `partial` mode they are compared by top then bottom (with left/height/width/DOM as fallback). Also updated an explanatory comment to reflect the different activation points.
@douden
Copy link
Copy Markdown
Member Author

douden commented Apr 8, 2026

I changed the README and added the swap. Can you test and review?

@Tom-van-Woudenberg Tom-van-Woudenberg merged commit 3bb6a39 into main Apr 8, 2026
3 checks passed
@Tom-van-Woudenberg Tom-van-Woudenberg deleted the trigger_and_figlxass branch April 8, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change trigger

2 participants