Skip to content
Snippets Groups Projects

Issue #3521869 by mably, thamas: Allow usage of a CSS variable for the sticky offset setting

Merged Issue #3521869 by mably, thamas: Allow usage of a CSS variable for the sticky offset setting
Merged Frank Mably requested to merge issue/toc_js-3521869:3521869-allow-usage-of into 3.x
Files
5
+ 2
44
@@ -314,39 +314,6 @@
return 1;
}
/**
* Retrieves the value of a CSS property from a CSS variable.
*
* If the CSS variable contains a fallback value, it will be used
* if the CSS variable is not defined.
*
* @param {string} styleValueSetting - The value of the CSS variable or a string containing `var(--variable, fallback)`.
* @param {string} [containerSelector='body'] - The selector for the container element to retrieve the CSS variable from.
* @return {string} - The value of the CSS property or the fallback value if defined.
*/
function getStyleValueFromCssProperty(
styleValueSetting,
containerSelector = 'body',
) {
let styleValue = styleValueSetting;
// Check if it's a CSS variable reference with optional fallback.
const variableMatch = styleValueSetting.match(
/^var\((--[a-zA-Z0-9-_]+)(?:,\s*([^)]+))?\)$/,
);
if (variableMatch) {
const propertyName = variableMatch[1];
const fallback = variableMatch[2]?.trim();
const container = document.querySelector(containerSelector);
styleValue = getComputedStyle(container)
.getPropertyValue(propertyName)
.trim();
if (!styleValue && fallback) {
styleValue = fallback;
}
}
return styleValue;
}
/**
* @param {TocObject} tocObj - The tocObj object.
*/
@@ -360,15 +327,6 @@
$tocNav.find('ul').remove();
let lastLi = $tocNav[0];
// Determine scroll offset.
let scrollToOffset;
if (opts.scrollToOffset) {
scrollToOffset = getStyleValueFromCssProperty(
opts.scrollToOffset,
opts.container || 'body',
);
}
$headings.each((i, heading) => {
// Skip invisible headings if specified in configuration.
if (opts.skipInvisibleHeadings) {
@@ -385,8 +343,8 @@
}
// Apply scroll offset.
if (scrollToOffset) {
heading.style.scrollMarginTop = scrollToOffset;
if (opts.scrollToOffset) {
heading.style.scrollMarginTop = opts.scrollToOffset;
}
// build TOC item
Loading