Skip to content

Commit

Permalink
feat: auto scroll list item into view
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Mar 10, 2022
1 parent 5f49d67 commit 3e11dda
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/histoire/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"picocolors": "^1.0.0",
"pinia": "^2.0.11",
"sade": "^1.8.1",
"scroll-into-view-if-needed": "^2.2.29",
"shiki": "^0.10.1",
"vite-node": "^0.3.6",
"vue-router": "^4.0.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import type { Story } from '../../types'
import BaseListItemLink from '../base/BaseListItemLink.vue'
import { Icon } from '@iconify/vue'
import { computed, withDefaults } from 'vue'
import { computed, ref, withDefaults } from 'vue'
import { useRoute } from 'vue-router'
import { useScrollOnActive } from '../../util/scroll.js'
const props = withDefaults(defineProps<{
story: Story
Expand All @@ -14,10 +16,15 @@ const props = withDefaults(defineProps<{
const filePadding = computed(() => {
return (props.depth * 16) + 'px'
})
const route = useRoute()
const active = computed(() => route.params.storyId === props.story.id)
const el = ref<HTMLDivElement>()
useScrollOnActive(active, el)
</script>

<template>
<div>
<div ref="el">
<BaseListItemLink
v-slot="{ active }"
:to="{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script lang="ts" setup>
import { PropType, toRefs } from 'vue'
import { PropType, ref, toRefs } from 'vue'
import { useCurrentVariantRoute } from '../../util/variant'
import type { Variant } from '../../types'
import BaseListItemLink from '../base/BaseListItemLink.vue'
import { Icon } from '@iconify/vue'
import { useScrollOnActive } from '../../util/scroll.js'
const props = defineProps({
variant: {
Expand All @@ -14,10 +15,12 @@ const props = defineProps({
const { variant } = toRefs(props)
const { isActive, targetRoute } = useCurrentVariantRoute(variant)
const el = ref<HTMLDivElement>()
useScrollOnActive(isActive, el)
</script>

<template>
<div>
<div ref="el">
<BaseListItemLink
v-slot="{ active }"
:to="targetRoute"
Expand Down
27 changes: 27 additions & 0 deletions packages/histoire/src/client/app/util/scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { watch, onMounted, Ref } from 'vue'
import scrollIntoView from 'scroll-into-view-if-needed'

export function useScrollOnActive (active: Ref<boolean>, el: Ref<HTMLElement>) {
watch(active, value => {
if (value) {
autoScroll()
}
})

function autoScroll () {
if (el.value) {
scrollIntoView(el.value, {
scrollMode: 'if-needed',
block: 'center',
inline: 'nearest',
behavior: 'smooth',
})
}
}

onMounted(() => {
if (active.value) {
autoScroll()
}
})
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e11dda

Please sign in to comment.