Skip to content

Commit ccd601b

Browse files
committed
Updated readme
- Fixed some typescript errors
1 parent 09231bd commit ccd601b

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vue3-sticky-directive
22

3-
vue3-sticky-directive is a powerful vue 3 directive make element sticky.
3+
Vue3-sticky-directive is a powerful Vue 3 directive make element sticky. This is an implementation of the [vue-sticky-directive](https://github.com/mehwww/vue-sticky-directive) package for Vue 2.
44

55
# Install
66

@@ -18,13 +18,11 @@ const app = createApp({
1818
app.use(Sticky)
1919
app.mount('#app')
2020

21-
Vue.use(Sticky)
22-
2321
```
2422

2523
# Usage
2624

27-
Use `v-sticky` directive to enable element postion sticky, and use `sticky-*` attributes to define its options. Sticky element will find its nearest element with `sticky-container` attribute or its parent node if faild as the releative element.
25+
Use `v-sticky` directive to enable element position sticky, and use `sticky-*` attributes to define its options. Sticky element will find its nearest element with `sticky-container` attribute or its parent node if failed as the relative element.
2826

2927
```HTML
3028
<div sticky-container>
@@ -35,6 +33,7 @@ Use `v-sticky` directive to enable element postion sticky, and use `sticky-*` at
3533
```
3634

3735
# Options
36+
3837
* `sticky-offset` - set sticky offset, it support a vm variable name or a js expression like `{top: 10, bottom: 20}`
3938
* `top`_(number)_ - set the top breakpoint (default: `0`)
4039
* `bottom`_(number)_ - set the bottom breakpoint (default: `0`)
@@ -51,7 +50,7 @@ Use `v-sticky` directive to enable element postion sticky, and use `sticky-*` at
5150
}
5251
```
5352

54-
An expression that evaluates to false set on `v-sticky` can be used to disable stickiness condtionally.
53+
An expression that evaluates to false set on `v-sticky` can be used to disable stickiness conditionally.
5554

5655
```HTML
5756
<div sticky-container>
@@ -61,11 +60,23 @@ An expression that evaluates to false set on `v-sticky` can be used to disable s
6160
</div>
6261
```
6362
```JavaScript
64-
export defaults {
65-
data () {
63+
import { defineComponent, ref } from 'vue'
64+
65+
// Options API
66+
defineComponent({
67+
data() {
6668
shouldStick: false
6769
}
68-
}
70+
})
71+
72+
// or with the Composition API
73+
defineComponent({
74+
setup() {
75+
const shouldStick = ref(false)
76+
77+
return { shouldStick }
78+
}
79+
})
6980
```
7081

7182
# License

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
22
"name": "vue3-sticky-directive",
3+
"author": "DapperBenji <[email protected]>",
34
"version": "0.0.1",
45
"type": "module",
56
"files": [
67
"dist"
78
],
89
"exports": {
9-
".": "./dist/index.es.js"
10+
".": "./dist/index.es.js"
1011
},
1112
"license": "MIT",
1213
"types": "./index.d.ts",
1314
"repository": {
1415
"type": "git",
1516
"url": "https://github.com/DapperBenji/vue3-sticky-directive.git"
1617
},
18+
"keywords": [
19+
"vue",
20+
"sticky",
21+
"directive"
22+
],
1723
"scripts": {
1824
"dev": "vite",
1925
"build": "vue-tsc --noEmit && vite build",

src/sticky.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ComponentPublicInstance } from 'vue'
22
import { State, LastState, Options, ElementStyle, ElementClassName } from './types'
33

4+
type stickySide = 'top' | 'bottom' | 'both'
5+
46
const namespace = '@@vue3-sticky-directive'
57
const events = [
68
'resize',
@@ -14,14 +16,11 @@ const events = [
1416

1517
const batchStyle = (el: HTMLElement, style: ElementStyle = {}, className: ElementClassName = {}): void => {
1618
for (let k in style) {
17-
/* @ts-ignore */
1819
el.style[k] = style[k]
1920
}
2021
for (let k in className) {
21-
/* @ts-ignore */
2222
if (className[k] && !el.classList.contains(k)) {
2323
el.classList.add(k)
24-
/* @ts-ignore */
2524
} else if (!className[k] && el.classList.contains(k)) {
2625
el.classList.remove(k)
2726
}
@@ -31,7 +30,7 @@ const batchStyle = (el: HTMLElement, style: ElementStyle = {}, className: Elemen
3130
class Sticky {
3231
el: HTMLElement
3332
vm: any// ComponentPublicInstance
34-
unsubscribers: Array<()=> void>
33+
unSubscribers: Array<()=> void>
3534
isPending: boolean
3635
state: State
3736
lastState: LastState
@@ -42,7 +41,7 @@ class Sticky {
4241
constructor (el: HTMLElement, vm: ComponentPublicInstance) {
4342
this.el = el
4443
this.vm = vm
45-
this.unsubscribers = []
44+
this.unSubscribers = []
4645
this.isPending = false
4746
this.state = {
4847
isTopSticky: null,
@@ -59,7 +58,7 @@ class Sticky {
5958
}
6059

6160
const offset = this.getAttribute('sticky-offset') || {}
62-
const side: string = this.getAttribute('sticky-side') || 'top'
61+
const side: stickySide = this.getAttribute('sticky-side') || 'top'
6362
const zIndex: string = this.getAttribute('sticky-z-index') || '10'
6463
const onStick = this.getAttribute('on-stick') || null
6564

@@ -74,7 +73,7 @@ class Sticky {
7473
}
7574

7675
doBind(): void {
77-
if (this.unsubscribers.length > 0) {
76+
if (this.unSubscribers.length > 0) {
7877
return
7978
}
8079

@@ -85,8 +84,8 @@ class Sticky {
8584
el.parentElement!.insertBefore(this.placeholderEl, el)
8685
events.forEach(event => {
8786
const fn = this.update.bind(this)
88-
this.unsubscribers.push(() => window.removeEventListener(event, fn))
89-
this.unsubscribers.push(() =>
87+
this.unSubscribers.push(() => window.removeEventListener(event, fn))
88+
this.unSubscribers.push(() =>
9089
this.containerEl!.removeEventListener(event, fn)
9190
)
9291
window.addEventListener(event, fn, { passive: true })
@@ -96,8 +95,8 @@ class Sticky {
9695
}
9796

9897
doUnbind(): void {
99-
this.unsubscribers.forEach(fn => fn())
100-
this.unsubscribers = []
98+
this.unSubscribers.forEach(fn => fn())
99+
this.unSubscribers = []
101100
this.resetElement()
102101
}
103102

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"isolatedModules": true,
1212
"esModuleInterop": true,
1313
"lib": ["ESNext", "DOM"],
14+
"suppressImplicitAnyIndexErrors": true,
1415
"skipLibCheck": true
1516
},
1617
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],

0 commit comments

Comments
 (0)