diff --git a/packages/docs/components/molecules/AnchorNav/stories/app.twig b/packages/docs/components/molecules/AnchorNav/stories/app.twig index af44c5e1..ba6f9eb6 100644 --- a/packages/docs/components/molecules/AnchorNav/stories/app.twig +++ b/packages/docs/components/molecules/AnchorNav/stories/app.twig @@ -22,23 +22,8 @@ {% endfor %} -
-
-
- Item-1 -
-
-
-
- Item-2 -
-
-
-
- Item-3 -
-
- {# {% for item in 1..section_length %} +
+ {% for item in 1..section_length %}
@@ -46,7 +31,7 @@ Item-{{ loop.index }}
- {% endfor %} #} + {% endfor %}
diff --git a/packages/ui/molecules/AnchorNav/AnchorNavTarget.ts b/packages/ui/molecules/AnchorNav/AnchorNavTarget.ts index 0bb45973..c731a7d9 100644 --- a/packages/ui/molecules/AnchorNav/AnchorNavTarget.ts +++ b/packages/ui/molecules/AnchorNav/AnchorNavTarget.ts @@ -1,4 +1,4 @@ -import { Base } from '@studiometa/js-toolkit'; +import { Base, withIntersectionObserver } from '@studiometa/js-toolkit'; import type { BaseProps, BaseConfig } from '@studiometa/js-toolkit'; export interface AnchorNavTargetProps extends BaseProps { @@ -10,65 +10,26 @@ export interface AnchorNavTargetProps extends BaseProps { /** * Manage a sticky table section. */ -export class AnchorNavTarget extends Base { +export class AnchorNavTarget extends withIntersectionObserver(Base, { + threshold: 0.5, + rootMargin: '0% 0px -40% 0px', +}) { /** * Config. */ static config: BaseConfig = { name: 'AnchorNavTarget', - refs: ['item[]'], - options: { - threshold: { type: String, default: '0.5' }, - rootmargin: { type: String, default: '0% 0px -40% 0px' }, - }, emits: ['is-intersected'], }; - mounted() { - console.log('mounted'); - - const observer = new IntersectionObserver(this.checkIntersection.bind(this), { - threshold: this.$options.threshold, - rootMargin: this.$options.rootmargin, - }); - this.$refs.item.forEach((target) => { - observer.observe(target); - }); - } - - checkIntersection(entries) { + intersected(entries) { console.log(entries); - if (entries[0].isIntersecting) { + console.log(this.$options); + let { id } = entries[0].target; id = `#${id}`; - console.log(id); - this.$emit('is-intersected', id); } } } - -/** - * Manage a sticky table section. - */ -// export class AnchorNavTarget extends withIntersectionObserver(Base, { -// threshold: 0.5, -// rootMargin: '0% 0px -40% 0px', -// }) { -// /** -// * Config. -// */ -// static config: BaseConfig = { -// name: 'AnchorNavTarget', -// }; - - // intersected(entries) { - // if (entries[0].isIntersecting) { - // console.log(this.$options); - - // let { id } = entries[0].target; - // id = `#${id}`; - // this.$emit('is-intersected', id); - // } -// }