Skip to content

Commit

Permalink
📖 Fill guide docs, part of core. Fix typos, ♻️ README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
somespecialone committed Oct 17, 2023
1 parent a4d667d commit ed06a20
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 124 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
[![license](https://img.shields.io/github/license/somespecialone/vuecomotive-scroll)](https://github.com/somespecialone/vuecomotive-scroll/blob/master/LICENSE)
[![npm](https://img.shields.io/npm/v/vuecomotive-scroll)](https://www.npmjs.com/package/vuecomotive-scroll)
[![Docs](https://github.com/somespecialone/vuecomotive-scroll/actions/workflows/docs.yml/badge.svg)](https://github.com/somespecialone/vuecomotive-scroll/actions/workflows/docs.yml)
[![Publish](https://github.com/somespecialone/vuecomotive-scroll/actions/workflows/publish.yml/badge.svg)](https://github.com/somespecialone/vuecomotive-scroll/actions/workflows/publish.yml)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat)](https://github.com/prettier/prettier)

Vuecomotive Scroll is a thin [Vue.js](https://vuejs.org) wrapper for
precious [Locomotive Scroll v5](https://github.com/locomotivemtl/locomotive-scroll) 🚂.

It utilizes native `Vue` features like components and composables to give max control of `Locomotive Scroll` API to
developer and make easier to integrate it in `Vue` app.
It utilizes native `Vue` features such as components and composables to give maximum control over `LocomotiveScroll` API
to developer and make it easier to integrate it into a `Vue` app.

[Documentation 📖](https://vuecomotive.somespecial.one)

---

⚠️ Project is `unstable` and depends
on [Locomotive Scroll v5 beta](https://github.com/locomotivemtl/locomotive-scroll/tree/v5-beta). So there maybe some
breaking changes in future unless stable (first major) version/release. Kind regards!
⚠️ The project is unstable and depends
on [Locomotive Scroll v5 beta](https://github.com/locomotivemtl/locomotive-scroll/tree/v5-beta).
So there might be some breaking changes in the future unless stable (first major) version is released. Best regards!

---

Expand All @@ -48,7 +49,7 @@ yarn add vuecomotive-scroll
Specified version

```html
<script src="https://unpkg.com/vuecomotive-scroll@0.2.0"></script>
<script src="https://unpkg.com/vuecomotive-scroll@0.2.1"></script>
```

Latest
Expand Down Expand Up @@ -123,8 +124,7 @@ function handleClickOnSomeElement({target}) {
## Core

* `scroll` - 🚂.
* `<ScrollView />` - wrapper for scrollable scene. It can be whole document or some container
element inside DOM.
* `<ScrollView />` - wrapper for scrollable scene. It can be whole document or any container element inside the DOM.
* `<ScrollComponent />` - `scroll element` within scroll scene. It gathers all `data-*`
attributes from [Locomotive Scroll element](https://scroll.locomotive.ca/docs/#/attributes).
* `useScroll` - composable that returns `scroll`, `scrollTo` function, refs with scroll data.
Expand Down
2 changes: 2 additions & 0 deletions docs/.vitepress/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEF_DUR = 1
export const PROGRESS_THRESHOLD = 0.95
11 changes: 6 additions & 5 deletions docs/.vitepress/theme/components/ScrollLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import HomeLogo from './HomeLogo.vue'
import MouseIco from './MouseIco.vue'
import ArrowIco from './ArrowIco.vue'
const PROGRESS_THRESHOLD = 0.95
import { DEF_DUR, PROGRESS_THRESHOLD } from '../../constants'
const { Layout } = DefaultTheme
Expand All @@ -20,15 +20,15 @@ const data = useData()
const frontScroll = computed<boolean>(() => data.frontmatter.value.scroll)
const home = computed(() => route.path == '/')
const duration = ref(1.2)
const duration = ref(DEF_DUR)
const durationComp = computed(() => (home.value ? 2 : duration.value))
const active = ref(false)
const visited = ref(false)
watch(route, () => {
visited.value = true
active.value = true
immediate = true
duration.value = 1.2
duration.value = DEF_DUR
})
// mock `window.scrollTo` function with ours `scrollTo` to handle scroll from router by ourselves
Expand Down Expand Up @@ -65,7 +65,7 @@ provide('duration', duration)
</script>

<template>
<ScrollView v-if="frontScroll" :duration="durationComp" root>
<ScrollView v-if="frontScroll" :duration="durationComp" root smooth-touch>
<ClientOnly v-if="home">
<Layout :class="{ active, home }">
<template #layout-top>
Expand Down Expand Up @@ -124,8 +124,9 @@ provide('duration', duration)
left: 50%;
color: var(--vp-c-text-2);
font-size: 10px;
font-size: clamp(5px, 0.6rem, 1rem);
font-weight: 500;
white-space: nowrap;
transform: translate(-50%, 100%);
transition: transform 0.5s ease-out;
Expand Down
4 changes: 2 additions & 2 deletions docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import 'vuecomotive-scroll/dist/lenis.css'
import createVuecomotiveScroll from 'vuecomotive-scroll'

// @ts-ignore
import IndexLayout from './components/ScrollLayout.vue'
import Layout from './components/ScrollLayout.vue'
// @ts-ignore
import DocButton from './components/DocButton.vue'

export default <Theme>{
extends: DefaultTheme,
Layout: IndexLayout,
Layout,
enhanceApp({ app }) {
app.component('DocButton', DocButton)

Expand Down
83 changes: 78 additions & 5 deletions docs/core/scroll-component.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,80 @@
---
scroll: true
---

# Scroll Component

| Property name | Type | Reactive | Reference | Description |
|---------------|------|----------|-----------|-------------|
| | | | | |
| | | | | |
| | | | | |
Component wrapper. Gathering all `data-*` attributes from
[Locomotive Scroll element](https://scroll.locomotive.ca/docs/#/attributes).

Must be rendered inside [ScrollView](scroll-view).

## Component props

| Property name | Type | Reactive | Reference / Default value | Description |
|---------------|------------------|:--------:|--------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| is | string/Component | - | `div` | Specifies which component to render. Same as `is` prop on [component](https://vuejs.org/api/built-in-special-elements.html#component). |
| inViewClass | string | + | `is-inview` | Specifies which class to apply to an element when it is in view. |
| position | string | + | [data-scroll-position](https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-position) | ⬅️ |
| offset | string | - | [data-scroll-offset](https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-offset) | ⬅️ |
| repeat | boolean | - | [data-scroll-repeat](https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-repeat) | ⬅️ |
| speed | number | - | [data-scroll-speed](https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-speed) | ⬅️ |
| cssProgress | boolean | + | [data-scroll-css-progress](https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-css-progress) | ⬅️ |
| ignoreFold | boolean | + | [data-scroll-ignore-fold](https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-ignore-fold) | ⬅️ |
| touchSpeed | boolean | + | [data-enable-touch-speed](https://scroll.locomotive.ca/docs/#/attributes?id=data-enable-touch-speed) | ⬅️ |

## Events

Emit `progress` and `intersect` events.
`progress` event handler will
take [IProgressEventPayload](https://github.com/somespecialone/vuecomotive-scroll/blob/master/lib/src/types.ts)
and `intersect` event handler will
take [IIntersectEventPayload](https://github.com/somespecialone/vuecomotive-scroll/blob/master/lib/src/types.ts)

```vue {2,4-6,8-10,14,15}
<script setup lang="ts">
import type { IProgressEventPayload, IIntersectEventPayload } from "vuecomotive-scroll";
function handleProgress({ target, progress }: IProgressEventPayload) {
// some work...
}
function handleIntersect({ target, way, from }: IIntersectEventPayload) {
// and there
}
</script>
<template>
<ScrollComponent @progress="handleProgress" />
<ScrollComponent @intersect="handleIntersect" />
</template>
```

## Expose

* `inView` - ref with boolean
* `el` - ref to HTMLElement
* `scrollElement` - [ScrollElement](https://github.com/locomotivemtl/locomotive-scroll/blob/v5-beta/src/core/ScrollElement.ts)

Pass only `inView` to slot via `v-slot`.

```vue {6-8,13}
<script setup lang="ts">
import { onMounted } from "vue"
const comp = ref()
onMounted(() => {
comp.inView
comp.el
comp.scrollElement
})
</script>
<template>
<ScrollComponent ref="comp" v-slot="{ inView }">
<!-- slot components -->
</ScrollComponent>
</template>
```
75 changes: 75 additions & 0 deletions docs/core/scroll-view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
scroll: true
---

# Scroll View

Main component.

:::warning Layout change
Due to [Locomotive Scroll](https://scroll.locomotive.ca/docs) implementation it is not possible to change layout
so don't use dynamic rendering or change width/height properties of elements inside `ScrollView`.
:::

## Props

| Property name | Type | Reactive | Default value | Description |
|---------------|------------------|:--------:|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| root | boolean | - | *false* | Specifies when the component will wrap and listen to the scroll event on the entire document or create wrapper & content elements. |
| cssProgress | boolean | + | *false* | Specifies whether or not to display the progress value on the root element in the `--wrapper-progress` CSS custom property (variable). Possible values are 0 to 1. |
| cssDirection | boolean | + | *false* | Same as `cssProgress` ⬆️, but in `--scroll-direction` property. Possible values are -1 (up), 1 (down). |
| wrapperIs | string/Component | - | `div` | Specifies which component to render. Same as `is` prop on [component](https://vuejs.org/api/built-in-special-elements.html#component). |
| contentIs | string/Component | - | `div` | ⬆️ |

Other props are [options from Lenis](https://scroll.locomotive.ca/docs/#/options?id=lenisoptions) and non-reactive.

## Events

Emit `lenis-scroll` event. Single argument passed to handler
is [Lenis](https://github.com/studio-freight/lenis/blob/main/dist/types/index.d.ts).

```vue {2,4-6,10}
<script setup lang="ts">
import type Lenis from '@studio-freight/lenis';
function onLenisScroll(lenis: Lenis) {
// do what you want
}
</script>
<template>
<ScrollView @lenis-scroll="onLenisScroll">
<!-- content -->
</ScrollView>
</template>
```

## Refs to elements

If you need ref to wrapper or/and content elements you can find it on `Lenis` instance after scroll is ready.

```vue {10-14}
<script setup lang="ts">
import { watch } from "vue";
import { useScroll } from "vuecomotive-scroll";
const { scroll, isReady } = useScroll();
watch(isReady, (r) => {
if (r) {
scroll.lenis.rootElement
// or
scroll.lenis.options.wrapper
scroll.lenis.options.content
}
})
</script>
```

:::tip onMounted
Or if you need it eagerly you can get it from `scroll.lenisOptionsGetter` when `ScrollView` is mounted.
:::
8 changes: 8 additions & 0 deletions docs/core/scroll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
scroll: true
---

# Scroll instance

Class that extends [LocomotiveScroll](https://github.com/locomotivemtl/locomotive-scroll/blob/v5-beta/src/index.ts).

17 changes: 17 additions & 0 deletions docs/core/use-scroll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
scroll: true
---

# Composable

For use inside `setup` of components only.

```vue
<script setup lang="ts">
import { useScroll } from "vuecomotive-scroll"
const { scrollTo, scroll, direction, isScrolling, isReady } = useScroll()
</script>
```

0 comments on commit ed06a20

Please sign in to comment.