1 | ## Installation
|
2 | ```bash
|
3 | $ npm install @renatodeleao/vue-sticky-directive
|
4 | ```
|
5 | or
|
6 | ```bash
|
7 | $ yarn add @renatodeleao/vue-sticky-directive
|
8 | ```
|
9 |
|
10 | ## Usage
|
11 | 👁 [Docs + Example](https://renatodeleao.github.io/vue-sticky-directive/)
|
12 | ⚠️ hands on code people: [codesandbox demo](https://codesandbox.io/s/mm4olmxkrx)
|
13 |
|
14 | #### Global register
|
15 | ```javascript
|
16 | import VueStickyDirective from '@renatodeleao/vue-sticky-directive'
|
17 | Vue.use(VueStickyDirective)
|
18 | // by default exposes v-sticky directive namespace
|
19 | ```
|
20 |
|
21 | #### Component register (recommended)
|
22 | ```javascript
|
23 | import VueStickyDirective from '@renatodeleao/vue-sticky-directive'
|
24 |
|
25 | export default {
|
26 | name: "your-component-name",
|
27 | /**
|
28 | * You can use alternative namespace instead of "sticky" here
|
29 | */
|
30 | directives: {
|
31 | "sticky": VueStickyDirective
|
32 | }
|
33 | }
|
34 | ```
|
35 |
|
36 | #### Recommended Markup
|
37 | ```HTML
|
38 | <!--sticky container (optional) -->
|
39 | <div class="your-container-class" data-v-sticky-container>
|
40 | <!-- the actual sticky element -->
|
41 | <div class="your-sidebar-class" v-sticky>
|
42 | <!-- where plugin applies transforms (optional) -->
|
43 | <div class="your-sidebar-inner-class" data-v-sticky-inner>
|
44 | </div>
|
45 | </div>
|
46 | ```
|
47 | <small>Note that `[data-v-sticky-container]` and `[data-v-sticky-inner]` are optional attributes. The first specify the `containerSelector`, boundary element to limit the begin and end points of sticky element. It defaults to closest parent if not present. The latter defines `innerWrapperSelector` of sticky sidebar, if this wrapper is not found inside `v-sticky` element, the plugin will create one for you under class name `inner-wrapper-sticky`. It's recommended element to apply your CSS styles.</small>
|
48 |
|
49 | #### ResizeSensor (Highly Recommended)
|
50 | I've (maybe naively) included ResizeSensor as a dependency of this package, albeight it's usage is optional. Note that by default, this plugin only re-calculates at `window.resize`. At original plugin's documentation, resizeSensor usage is also recommended. The the thing is, if you don't include this, you have to manually detect parent and el resizes and call `this.el._stickySidebar.updateSticky()` yourself or dispatching dom `resize` events yourself, because at the time of mounting the directive, your parent container might be still loading content or other nested components might not have mounted yet, therefore at the computed height at that time might be wrong.
|
51 |
|
52 | ```javascript
|
53 | // anywhere before registering directive, only once globally
|
54 | import ResizeSensor from "resize-sensor"
|
55 | window.ResizeSensor = ResizeSensor // [1]
|
56 | ```
|
57 | <small>[1] - The reason to polute global namespace is that [original plugin](https://github.com/abouolia/sticky-sidebar/blob/master/src/sticky-sidebar.js#L199) uses this reference as condition verification to create the resizeSensors.</small>
|
58 |
|
59 | ## Options
|
60 | Same options as [original plugin](https://abouolia.github.io/sticky-sidebar/#options), with the exception of default selectors for `containerSelector` and `innerWrapperSelector`, that use `data-attributes` now, a personal preference for separation of concerns.
|
61 |
|
62 | ```javascript
|
63 | {
|
64 | topSpacing: 0,
|
65 | bottomSpacing: 0,
|
66 | containerSelector: "[data-v-sticky-container]",
|
67 | innerWrapperSelector: "[data-v-sticky-inner]",
|
68 | resizeSensor: true, // [1] read above
|
69 | stickyClass: "is-affixed",
|
70 | minWidth: 0
|
71 | };
|
72 | ```
|
73 | And should be passed to the `v-sticky` directive binding value.
|
74 |
|
75 | ```HTML
|
76 | <template>
|
77 | <div data-v-sticky-container>
|
78 | <!-- the actual sticky element -->
|
79 | <div v-sticky="options">
|
80 | <!-- where plugin applies transforms -->
|
81 | <div data-v-sticky-inner>
|
82 | </div>
|
83 | </div>
|
84 | </template>
|
85 |
|
86 | <script>
|
87 | export default {
|
88 | ...
|
89 | data(){
|
90 | return {
|
91 | options: {
|
92 | topSpacing: 20
|
93 | }
|
94 | }
|
95 | }
|
96 | }
|
97 | </script>
|
98 | ```
|
99 |
|
100 | <small>Note: do-not use `:v-sticky` to bind values, it's not the way directives work.</small>
|
101 |
|
102 | ## Events
|
103 | Same events as [original plugin](https://abouolia.github.io/sticky-sidebar/#events) and are available using the standard Vue `v-on:event-name` or `@event-name` notation. The event emits an `Object` containing `evtName` and `vnode` allowing access for custom manipulation (ex: adding a specific class).
|
104 |
|
105 | ```HTML
|
106 | <template>
|
107 | <div data-v-sticky-container>
|
108 | <div v-sticky="options" @affix-top="handleStickyEvent">
|
109 | <div data-v-sticky-inner>
|
110 | </div>
|
111 | </div>
|
112 | </template>
|
113 |
|
114 | <script>
|
115 | export default {
|
116 | ...
|
117 | methods:{
|
118 | handleStickyEvent(payload){
|
119 | console.log(payload);
|
120 | // {evtName: "affix-top", vnode: vnode}
|
121 | payload.vnode.elm.classList('you-reached-the-top');
|
122 | }
|
123 | }
|
124 | }
|
125 | </script>
|
126 | ```
|
127 | ```javascript
|
128 | EVENT_NAMES = [
|
129 | "affix-top",
|
130 | "affixed-top",
|
131 | "affix-bottom",
|
132 | "affixed-bottom",
|
133 | "affix-container-bottom",
|
134 | "affixed-container-bottom",
|
135 | "affix-unbottom",
|
136 | "affixed-unbottom",
|
137 | "affix-static",
|
138 | "affixed-static"
|
139 | ]
|
140 | ```
|
141 | The "ed" suffix denotes *after* event whereas the unsuffixed denotes *before*.
|
142 |
|
143 | ## Develop and Contribute
|
144 |
|
145 | ```bash
|
146 | $ git clone this repo
|
147 | # install dev dependencies
|
148 | $ npm install
|
149 | # rollup with watch mode
|
150 | $ npm run dev
|
151 | # test app
|
152 | $ npm run parcel
|
153 | # build lib
|
154 | $ npm run build
|
155 | ```
|
156 |
|
157 | ## Motivations, credits and thanks
|
158 | I didn't kept Aboulia's original name, because you can make any type of sticky element with this. The reason to use its plugin it's performance and the overflow behaviour (scroll-past-and-affix-[bottom/up]).
|
159 |
|
160 | Special thanks to [@abouolia](https://github.com/abouolia/) for taking time to develop this, and to [@mehwww](https://github.com/mehwww/) to point me the way to build this wrapper.
|
161 |
|
162 | ## Other sticky libraries
|
163 | - [@mehwww](https://github.com/mehwww/) has [lightweight sticky directive](https://www.npmjs.com/package/vue-sticky-directive)
|
164 |
|
165 | ## License
|
166 | MIT |
\ | No newline at end of file |