@sikka/hawa 0.0.260 → 0.0.261

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sikka/hawa",
3
- "version": "0.0.260",
3
+ "version": "0.0.261",
4
4
  "description": "SaaS Oriented UI Kit",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.es.js",
@@ -1,4 +1,4 @@
1
- import React, { FC, RefObject, useState, useEffect } from "react"
1
+ import React, { FC, RefObject, useState, useEffect, useRef } from "react"
2
2
  import { clsx } from "clsx"
3
3
 
4
4
  type ComponentTypes = {
@@ -11,6 +11,14 @@ type ComponentTypes = {
11
11
 
12
12
  export const BackToTop: FC<ComponentTypes> = ({ ...props }) => {
13
13
  const [visible, setVisible] = useState<boolean>(false)
14
+ const [rect, _setRect] = useState<DOMRect>(null)
15
+ const _rect = useRef(rect)
16
+ const setRect = (data) => {
17
+ _rect.current = data
18
+ _setRect(data)
19
+ }
20
+
21
+ const self = useRef(null)
14
22
 
15
23
  const getCoords = () => {
16
24
  let anchor = props.anchor.current
@@ -29,42 +37,59 @@ export const BackToTop: FC<ComponentTypes> = ({ ...props }) => {
29
37
  props.anchor.current.scrollTo(0, 0)
30
38
  }
31
39
 
40
+ // FIXME: Observers and listeners run twice
32
41
  useEffect(() => {
42
+ if (!props.anchor.current) return
43
+
33
44
  props.anchor.current.addEventListener("scroll", onScroll)
34
45
 
46
+ // Listens to rect changes. Alternatives like ResizeObserver & IntersectionObserver fail to detect positional changes consistently
47
+ let interval = setInterval(() => {
48
+ if (!props.anchor.current) return
49
+
50
+ let newRect = props.anchor.current.getBoundingClientRect()
51
+ if (_rect.current == null) return setRect(newRect)
52
+
53
+ if (
54
+ !(
55
+ _rect.current.top == newRect.top &&
56
+ _rect.current.left == newRect.left &&
57
+ _rect.current.width == newRect.width &&
58
+ _rect.current.height == newRect.height
59
+ )
60
+ ) {
61
+ setRect(newRect)
62
+ }
63
+ }, 1)
64
+
35
65
  return () => {
36
66
  props.anchor.current?.removeEventListener("scroll", onScroll)
67
+ clearInterval(interval)
37
68
  }
38
69
  }, [])
39
70
 
40
- // // Reference to tailwind classes of corners
41
- // const corners = {
42
- // "top-left": ["top-0 left-0", "ml", "mt"],
43
- // "top-right": ["top-0 right-0", "mr", "mt"],
44
- // "bottom-left": ["bottom-0 left-0", "ml", "mb"],
45
- // "bottom-right": ["bottom-0 right-0", "mr", "mb"],
46
- // }
47
-
48
- // const getClasses = () => {
49
- // let [corner, horizontal, vertical] = corners[props.corner || "bottom-right"]
50
-
51
- // return clsx(
52
- // `${horizontal}-[${props.paddingX || 100}px]`,
53
- // `${vertical}-[${props.paddingY || 0}px]`,
54
- // `${corner}`,
55
- // `${visible ? "block" : "hidden"}`
56
- // )
57
- // }
58
-
59
- // Had to use react styles because tailwind didn't update top & left
60
71
  const getStyles = () => {
72
+ if (!props.anchor.current) return {}
73
+
61
74
  let corner = props.corner || "bottom-right"
62
75
  let [vertical, horizontal] = corner.split("-")
63
76
 
64
- let style = {}
65
-
66
- style[vertical] = props.paddingY || 15
67
- style[horizontal] = props.paddingX || 30
77
+ let anchorRect = props.anchor.current.getBoundingClientRect()
78
+ let selfRect = self.current.getBoundingClientRect()
79
+
80
+ let width = horizontal == "right" ? anchorRect.width - selfRect.width : 0
81
+ let height = vertical == "bottom" ? anchorRect.height - selfRect.height : 0
82
+
83
+ let style = {
84
+ top:
85
+ anchorRect.y +
86
+ height +
87
+ (vertical == "bottom" ? -1 : 1) * (props.paddingX || 10),
88
+ left:
89
+ anchorRect.x +
90
+ width +
91
+ (horizontal == "right" ? -1 : 1) * (props.paddingX || 25),
92
+ }
68
93
 
69
94
  return style
70
95
  }
@@ -72,11 +97,19 @@ export const BackToTop: FC<ComponentTypes> = ({ ...props }) => {
72
97
  return (
73
98
  <div
74
99
  className={clsx(
75
- "fixed w-fit rounded bg-blue-300 p-2 text-black transition-all hover:bg-blue-500 ",
76
- `${visible ? "inline-block" : "hidden"}`
100
+ "fixed w-fit rounded bg-blue-300 p-2 text-black transition-all hover:bg-blue-500",
101
+ `${
102
+ visible
103
+ ? "pointer-events-all opacity-100"
104
+ : "pointer-events-none opacity-0"
105
+ }`
77
106
  )}
78
- style={getStyles()}
107
+ style={{
108
+ ...getStyles(),
109
+ transitionProperty: "opacity, background-color",
110
+ }}
79
111
  onClick={backToTop}
112
+ ref={self}
80
113
  >
81
114
  {/* Back to top arrow 👇*/}
82
115
  <svg
package/src/styles.css CHANGED
@@ -1042,6 +1042,9 @@ video {
1042
1042
  .w-9 {
1043
1043
  width: 2.25rem;
1044
1044
  }
1045
+ .w-\[12px\] {
1046
+ width: 12px;
1047
+ }
1045
1048
  .w-\[32px\] {
1046
1049
  width: 32px;
1047
1050
  }