@splendidlabz/styles 4.4.0 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@splendidlabz/styles",
3
- "version": "4.4.0",
3
+ "version": "4.4.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Zell Liew <zellwk@gmail.com>",
7
7
  "files": [
8
8
  "src",
9
- "dist"
9
+ "dist",
10
+ "scripts"
10
11
  ],
11
12
  "exports": {
12
13
  ".": "./src/styles.css",
package/scripts/all.js ADDED
@@ -0,0 +1,14 @@
1
+ import { masonry, scrollableHorizontal, sticky } from './index.js'
2
+
3
+ const defaultOptions = {
4
+ masonry: true,
5
+ sticky: true,
6
+ scrollable: true,
7
+ }
8
+
9
+ export function all(options = {}) {
10
+ options = { ...defaultOptions, ...options }
11
+ if (options.masonry) masonry(options.element)
12
+ if (options.sticky) sticky(options.element)
13
+ if (options.scrollable) scrollableHorizontal(options.element)
14
+ }
@@ -0,0 +1,3 @@
1
+ export * from './masonry.js'
2
+ export * from './scrollable.js'
3
+ export * from './sticky.js'
@@ -0,0 +1,7 @@
1
+ import { getElement, masonry as Masonry } from '@splendidlabz/utils/dom'
2
+
3
+ export function masonry(selector) {
4
+ const container = getElement(selector) || document
5
+ const elements = Array.from(container.querySelectorAll("[class*='masonry']"))
6
+ elements.forEach(Masonry)
7
+ }
@@ -0,0 +1,14 @@
1
+ import { getElement, preferHorizontalScroll } from '@splendidlabz/utils/dom'
2
+
3
+ export function scrollableHorizontal(selector) {
4
+ const container = getElement(selector) || document
5
+
6
+ const elements = Array.from(
7
+ container.querySelectorAll('.scrollable-prefer-horizontal'),
8
+ )
9
+
10
+ if (container.classList?.contains('scrollable-prefer-horizontal'))
11
+ elements.push(container)
12
+
13
+ elements.forEach(preferHorizontalScroll)
14
+ }
@@ -0,0 +1,35 @@
1
+ import {
2
+ getElement,
3
+ mutationObserver,
4
+ sticky as Sticky,
5
+ } from '@splendidlabz/utils/dom'
6
+
7
+ /**
8
+ * Initializes sticky behavior for elements within a container.
9
+ * @param {string|HTMLElement} selector - The selector or element to initialize sticky behavior within.
10
+ * @param {Object} [options] - Optional settings.
11
+ * @param {number} [options.observeTimeout=0] - The timeout in milliseconds to observe DOM mutations.
12
+ */
13
+ export function sticky(selector, { observerTimeout = 1000 } = {}) {
14
+ const container = getElement(selector) || document.body
15
+ const elements = Array.from(container.querySelectorAll('.sticky-check'))
16
+ if (container.classList.contains('sticky-check')) elements.push(container)
17
+
18
+ elements.forEach(Sticky)
19
+
20
+ if (observerTimeout) {
21
+ const obs = mutationObserver(container, {
22
+ childList: true,
23
+ subtree: true,
24
+ callback: function ({ entry, observer }) {
25
+ // Nodes seem to be added one by one, so getting the first one is enough
26
+ const node = entry.addedNodes[0]
27
+ const klass = node?.className
28
+ if (!klass || typeof klass !== 'string') return
29
+ if (!klass.includes('sticky-check')) return
30
+ Sticky(node)
31
+ },
32
+ })
33
+ if (observerTimeout) setTimeout(_ => obs.disconnect(), observerTimeout)
34
+ }
35
+ }