Skip to content

Commit 8b53ee6

Browse files
committed
feat(tolerance): introducing tolerance(Down|Up) props
The new properties: toleranceUp and toleranceDown override tolerance and can be used to further customize the pin/unpin triggers.
1 parent 5e46502 commit 8b53ee6

File tree

5 files changed

+178
-85
lines changed

5 files changed

+178
-85
lines changed

README.md

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ Svelte Headroom is a [Svelte](https://svelte.dev) component to hide or show your
1010

1111
[Svelte REPL](https://svelte.dev/repl/44cafd471bcf497080e12ed3bee80986?version=3.53.1)
1212

13-
[Grab a copy](https://github.com/taocode/svelte-headroom) and play with it directly. Run `pnpm dev` (or yarn, npm equivalents) and edit the demo test page: `src/routes/+page.svelte`
13+
## Source
14+
15+
[Get the source from the GitHub repository (@taocode/svelte-headroom)](https://github.com/taocode/svelte-headroom) and play with it directly. Run `pnpm dev` (or yarn, npm equivalents) and edit the demo test page: `src/routes/+page.svelte`
1416

1517
## Install
1618

17-
`pnpm install -D @taocode/svelte-headroom`
19+
One of these:
20+
21+
- `pnpm install -D @taocode/svelte-headroom`
22+
- `yarn add -D @taocode/svelte-headroom`
23+
- `npm install -D @taocode/svelte-headroom`
1824

1925
## Usage
2026

@@ -24,110 +30,145 @@ Svelte Headroom is a [Svelte](https://svelte.dev) component to hide or show your
2430
</script>
2531

2632
<Headroom>
27-
<!-- Header will be fixed at top and auto show/hide based on scroll -->
33+
<!-- Header will be fixed at top
34+
and auto show/hide based on scroll -->
2835
</Headroom>
29-
3036
```
3137

3238
## Props
3339

3440
### `tolerance` number
3541

36-
The number of pixels that need to be scrolled in either direction for the effect to occur. This is useful if you want the user to be able to scroll slowly and not change the header position. Default: `0`.
42+
The number of pixels that need to be scrolled in either direction for the effect to occur. This is useful if you want the user to be able to scroll slowly and not change the header position.
3743

38-
```html
44+
__default: `0`__ -- _Overridden by `toleranceDown` and `toleranceUp`_
3945

46+
```html
4047
<Headroom tolerance={10}>
41-
<!-- will ignore any scroll less than 10px -->
48+
<!-- ignore any scroll less than 10px -->
4249
</Headroom>
50+
```
51+
52+
#### `toleranceDown` number
53+
54+
The number of pixels that need to be scrolled down for the effect to occur. This is useful if you want the user to be able to scroll slowly but respond differently to the downward direction.
4355

56+
__default: `tolerance`__ -- _Overrides `tolerance`_
57+
58+
```html
59+
<Headroom toleranceDown={10}>
60+
<!-- ignore downward scroll less than 10px -->
61+
</Headroom>
4462
```
45-
### `offsetTop` number
4663

47-
The number of pixels from the top of the page before the effect is allowed to occur; *controls:* `class:atTop` (useful with hideAtTop, showAtTop). Default: `2`.
64+
#### `toleranceUp` number
65+
66+
The number of pixels that need to be scrolled up for the effect to occur. This is useful if you want the user to be able to scroll slowly but respond differently in the upward direction.
67+
68+
__default: `tolerance`__ -- _Overrides `tolerance`_
4869

4970
```html
71+
<Headroom toleranceUp={10}>
72+
<!-- ignore upward scroll less than 10px -->
73+
</Headroom>
74+
```
75+
76+
### `offset` number
77+
78+
The number of pixels from the top or bottom of the page before the effect is allowed to occur; *controls:* `class:atTop` and `class:atBottom` (useful with [(show|hide)At(Bottom|Top)](#ats)).
79+
80+
__default: `2`__ -- _Overridden by `offsetTop` and `offsetBottom`_
81+
82+
#### `offsetTop` number
83+
84+
The number of pixels from the top of the page before the effect is allowed to occur; *controls:* `class:atTop` (useful with hideAtTop, showAtTop).
5085

86+
__default: `offset`__ -- _Overrides `offset`_
87+
88+
```html
5189
<Headroom offsetTop={50}>
5290
<!-- will show until after 50px from top -->
5391
</Headroom>
54-
5592
```
5693

57-
### `offsetBottom` number
94+
#### `offsetBottom` number
5895

59-
The number of pixels from bottom to be considered at the bottom; *controls* `class:atBottom` (useful with hideAtBottom, showAtBottom). Default: `2`.
96+
The number of pixels from bottom to be considered at the bottom; *controls* `class:atBottom` (useful with hideAtBottom, showAtBottom).
6097

61-
```html
98+
__default: `offset`__ -- _Overrides `offset`_
6299

100+
```html
63101
<Headroom showAtBottom offsetBottom={150}>
64-
<!-- will show when within 150px of bottom -->
102+
<!-- show when within 150px of bottom -->
65103
</Headroom>
66-
67104
```
68105

69106
### `duration` string
70107

71-
The duration of the sliding effect. The value is passed on as a [CSS Transition Duration](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-duration). Default: `"300ms"`.
108+
The duration of the sliding effect. The value is passed on as a [CSS Transition Duration](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-duration).
72109

73-
```html
110+
__default: `"300ms"`__
74111

112+
```html
75113
<Headroom duration='500ms'>
76-
<!-- will take 500ms to transition -->
114+
<!-- take 500ms to transition -->
77115
</Headroom>
78-
79116
```
80117

81118
### `easing` string
82119

83-
The timing function (easing) of the sliding effect. The value is passed on as a [CSS Transition Timing Function](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function). Default: `"linear"`.
120+
The timing function (easing) of the sliding effect. The value is passed on as a [CSS Transition Timing Function](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function).
84121

85-
```html
122+
__default: `"linear"`__
86123

124+
```html
87125
<Headroom easing='ease-out'>
88126
<!-- will ease-out -->
89127
</Headroom>
90-
91128
```
92129

93130
### `bottom` boolean
94131

95-
If this is to be pinned to the bottom, like a return to top button. Default: `false`.
132+
If this is to be pinned to the bottom, like a return to top button.
133+
134+
__default: `false`__
96135

97136
*Note:* you'll need to wrap a bottom pinned Headroom to use it because
98137
it isn't 100% wide to avoid covering links and breaking the UI a bit.
99138
You can play with heights and widths and different positioning; *see below for my favorite .totop-wrap*
100139

101140
```html
102-
103141
<div class="totop-wrap">
104142
<Headroom bottom>
105143
<!-- pinned to the bottom -->
106144
</Headroom>
107145
</div>
108-
109146
```
110147

111-
### `(show|hide)At(Top|Bottom)` boolean
148+
### `(hide|show)At(Bottom|Top)` boolean
112149

113-
If this is to be pinned to the bottom, like a return to top button. Default: `false`.
150+
If this is to be pinned to the bottom, like a return to top button.
114151

115-
*Note:* bottom detection fails on [SVELTE REPL]().
152+
__default: `false`__
116153

117-
```html
154+
*Note:* atBottom detection fails on [Svelte REPL](https://svelte.dev/repl/44cafd471bcf497080e12ed3bee80986?version=3.53.1).
118155

156+
```html
119157
<Headroom showAtBottom>
120-
<!-- my header that will always show when at bottom -->
158+
<!-- always show when at bottom -->
121159
</Headroom>
122160

123161
<div class="totop-wrap">
124-
<Headroom bottom hideAtTop showAtBottom tolerance={20} shim={50}>
162+
<Headroom
163+
bottom hideAtTop showAtBottom
164+
toleranceUp={20} offset={50}>
125165
<!-- footer for 'to top' button -->
126166
<button
127167
class="totop"
128168
on:click={()=> window.scroll(0,0)}
129169
>
130-
<span class="caret">^</span> <span>To Top</span>
170+
<span class="caret">^</span>
171+
<span>To Top</span>
131172
</button>
132173
</Headroom>
133174
</div>
@@ -155,17 +196,16 @@ If this is to be pinned to the bottom, like a return to top button. Default: `fa
155196
}
156197
.caret {
157198
font-size: 4em;
158-
padding-top: 0.5rem;
199+
padding-top: 0.6rem;
159200
}
160201
</style>
161-
162202
```
163203

164204
## Events
165205

166206
A `svelte-headroom` component emits two events: `pin` and `unpin`.
167207

168-
```jsx
208+
```html
169209
<Headroom
170210
on:pin={ () => {} }
171211
on:unpin={ () => {} }

0 commit comments

Comments
 (0)