Skip to content

Commit

Permalink
release 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
smastrom committed Aug 17, 2023
1 parent b6e08c2 commit 24e1261
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ useFixedHeader(headerRef, {

## Customization

> Default values are displayed below.
```ts
const { styles, isEnter, isLeave } = useFixedHeader(headerRef, {
/**
Expand Down Expand Up @@ -168,15 +170,15 @@ const { styles, isEnter, isLeave } = useFixedHeader(headerRef, {
*
* Custom styles applied when scrolling up */
enterStyles: {
transition: `transform 0.3s ease-out`,
transition: `transform 0.3s cubic-bezier(0.16, 1, 0.3, 1)`,
transform: 'translateY(0px)',
},
/**
*
* Custom styles applied when scrolling down */
leaveStyles: {
transition: `transform 0.5s ease-out`,
transform: 'translateY(-100%)',
transition: `transform 0.5s cubic-bezier(0.16, 1, 0.3, 1)`,
transform: 'translateY(-101%)',
},
})
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vue-use-fixed-header",
"description": "Turn your boring fixed header into a smart one with three lines of code.",
"private": false,
"version": "1.0.0",
"version": "1.0.2",
"type": "module",
"keywords": [
"vue",
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Ref, ComputedRef, CSSProperties } from 'vue'
import type { Ref, ComputedRef, CSSProperties, ShallowRef } from 'vue'

export type MaybeTemplateRef = HTMLElement | null | Ref<HTMLElement | null>

Expand Down Expand Up @@ -31,7 +31,7 @@ export interface UseFixedHeaderOptions<T = any> {
}

export interface UseFixedHeaderReturn {
styles: CSSProperties
styles: ShallowRef<CSSProperties>
isLeave: ComputedRef<boolean>
isEnter: ComputedRef<boolean>
}
39 changes: 19 additions & 20 deletions src/useFixedHeader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
onBeforeUnmount,
shallowRef,
ref,
unref,
watch,
nextTick,
shallowReactive,
computed,
readonly,
type CSSProperties,
} from 'vue'

Expand Down Expand Up @@ -34,15 +34,15 @@ export function useFixedHeader(

// Internal state

const styles = shallowReactive<CSSProperties>({})
const styles = shallowRef<CSSProperties>({})
const state = ref<State>(State.READY)

function setStyles(newStyles: CSSProperties) {
Object.assign(styles, newStyles)
styles.value = newStyles
}

function removeStyles() {
Object.keys(styles).forEach((key) => delete styles[key as keyof CSSProperties])
styles.value = {}
}

function setState(newState: State) {
Expand All @@ -52,8 +52,6 @@ export function useFixedHeader(
// Target utils

function getRoot() {
if (isSSR) return null

const root = unref(mergedOptions.root)
if (root != null) return root

Expand Down Expand Up @@ -114,7 +112,9 @@ export function useFixedHeader(

setStyles({
...mergedOptions.enterStyles,
...(mergedOptions.toggleVisibility ? { visibility: undefined } : {}),
...(mergedOptions.toggleVisibility
? { visibility: '' as CSSProperties['visibility'] }
: {}),
...(isReducedMotion() ? { transition: 'none' } : {}),
})

Expand Down Expand Up @@ -227,7 +227,7 @@ export function useFixedHeader(
const root = getRoot()
if (!root) return

const scrollRoot = root === document.documentElement ? window : root
const scrollRoot = root === document.documentElement ? document : root
const method = isRemove ? 'removeEventListener' : 'addEventListener'

scrollRoot[method]('scroll', onScroll, { passive: true })
Expand All @@ -237,20 +237,19 @@ export function useFixedHeader(

// Pointer

const setHover = () => (isHovering = true)
const removeHover = () => (isHovering = false)
function setPointer(e: PointerEvent) {
isHovering = unref(target)?.contains(e.target as Node) ?? false
}

function togglePointer(isRemove = false) {
const method = isRemove ? 'removeEventListener' : 'addEventListener'

unref(target)?.[method]('pointermove', setHover)
unref(target)?.[method]('pointerleave', removeHover)
document[method]('pointermove', setPointer as EventListener)
}

// Listeners

function removeListeners() {
toggleTransitionListener(true)
toggleScroll(true)
togglePointer(true)
}
Expand All @@ -262,7 +261,7 @@ export function useFixedHeader(
// If the header is not anymore fixed or sticky
if (!isValid) {
removeListeners()
nextTick(removeStyles)
removeStyles()
}
// If was not listening and now is fixed or sticky
} else {
Expand Down Expand Up @@ -302,10 +301,10 @@ export function useFixedHeader(
*/
watch(
() => [unref(target), unref(mergedOptions.root)],
(targetEl, _, onCleanup) => {
if (isSSR) return
([targetEl, rootEl], _, onCleanup) => {
const shouldInit = !isSSR && targetEl && (rootEl || rootEl === null)

if (targetEl) {
if (shouldInit) {
/**
* Resize observer is added in any case as it is
* in charge of toggling scroll/pointer listeners if the header
Expand All @@ -316,7 +315,7 @@ export function useFixedHeader(

/**
* Immediately hides the header on page load, this has effect
* only if scroll restoration is not smooth and toggleVisibility
* only if scroll restoration is not smooth and 'toggleVisibility'
* is set to true.
*/
onInstantScrollRestoration()
Expand All @@ -338,7 +337,7 @@ export function useFixedHeader(
onBeforeUnmount(_onCleanup)

return {
styles,
styles: readonly(styles),
isLeave: computed(() => state.value === State.LEAVE),
isEnter: computed(() => state.value === State.ENTER),
}
Expand Down

0 comments on commit 24e1261

Please sign in to comment.