@ndla/ui 6.1.1 → 6.1.2

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.
@@ -17,11 +17,13 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
17
17
  * LICENSE file in the root directory of this source tree.
18
18
  *
19
19
  */
20
- import { useState } from 'react';
20
+ import { useEffect, useState } from 'react';
21
21
  import { resizeObserver } from '@ndla/util';
22
22
  export var getMastheadHeight = function getMastheadHeight() {
23
- var masthead = document && document.getElementById('masthead');
24
- return masthead === null || masthead === void 0 ? void 0 : masthead.getBoundingClientRect().height;
23
+ if (typeof window !== 'undefined') {
24
+ var masthead = document.getElementById('masthead');
25
+ return masthead === null || masthead === void 0 ? void 0 : masthead.getBoundingClientRect().height;
26
+ }
25
27
  };
26
28
  export var useMastheadHeight = function useMastheadHeight() {
27
29
  var _useState = useState(),
@@ -29,17 +31,28 @@ export var useMastheadHeight = function useMastheadHeight() {
29
31
  height = _useState2[0],
30
32
  setHeight = _useState2[1];
31
33
 
32
- var masthead = document && document.getElementById('masthead');
33
-
34
- var handleHeightChange = function handleHeightChange(el) {
35
- var newHeight = el.getBoundingClientRect().height;
36
- setHeight(newHeight);
37
- };
38
-
39
- if (masthead) {
40
- resizeObserver(masthead, handleHeightChange);
41
- }
42
-
34
+ var _useState3 = useState(false),
35
+ _useState4 = _slicedToArray(_useState3, 2),
36
+ mounted = _useState4[0],
37
+ setMounted = _useState4[1];
38
+
39
+ useEffect(function () {
40
+ setMounted(true);
41
+ }, []);
42
+ useEffect(function () {
43
+ if (mounted) {
44
+ var masthead = document.getElementById('masthead');
45
+
46
+ var handleHeightChange = function handleHeightChange(el) {
47
+ var newHeight = el.getBoundingClientRect().height;
48
+ setHeight(newHeight);
49
+ };
50
+
51
+ if (masthead) {
52
+ resizeObserver(masthead, handleHeightChange);
53
+ }
54
+ }
55
+ }, [mounted]);
43
56
  return {
44
57
  height: height
45
58
  };
@@ -22,8 +22,10 @@ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(
22
22
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
23
23
 
24
24
  var getMastheadHeight = function getMastheadHeight() {
25
- var masthead = document && document.getElementById('masthead');
26
- return masthead === null || masthead === void 0 ? void 0 : masthead.getBoundingClientRect().height;
25
+ if (typeof window !== 'undefined') {
26
+ var masthead = document.getElementById('masthead');
27
+ return masthead === null || masthead === void 0 ? void 0 : masthead.getBoundingClientRect().height;
28
+ }
27
29
  };
28
30
 
29
31
  exports.getMastheadHeight = getMastheadHeight;
@@ -34,17 +36,28 @@ var useMastheadHeight = function useMastheadHeight() {
34
36
  height = _useState2[0],
35
37
  setHeight = _useState2[1];
36
38
 
37
- var masthead = document && document.getElementById('masthead');
38
-
39
- var handleHeightChange = function handleHeightChange(el) {
40
- var newHeight = el.getBoundingClientRect().height;
41
- setHeight(newHeight);
42
- };
43
-
44
- if (masthead) {
45
- (0, _util.resizeObserver)(masthead, handleHeightChange);
46
- }
47
-
39
+ var _useState3 = (0, _react.useState)(false),
40
+ _useState4 = _slicedToArray(_useState3, 2),
41
+ mounted = _useState4[0],
42
+ setMounted = _useState4[1];
43
+
44
+ (0, _react.useEffect)(function () {
45
+ setMounted(true);
46
+ }, []);
47
+ (0, _react.useEffect)(function () {
48
+ if (mounted) {
49
+ var masthead = document.getElementById('masthead');
50
+
51
+ var handleHeightChange = function handleHeightChange(el) {
52
+ var newHeight = el.getBoundingClientRect().height;
53
+ setHeight(newHeight);
54
+ };
55
+
56
+ if (masthead) {
57
+ (0, _util.resizeObserver)(masthead, handleHeightChange);
58
+ }
59
+ }
60
+ }, [mounted]);
48
61
  return {
49
62
  height: height
50
63
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndla/ui",
3
- "version": "6.1.1",
3
+ "version": "6.1.2",
4
4
  "description": "UI component library for NDLA.",
5
5
  "license": "GPL-3.0",
6
6
  "main": "lib/index.js",
@@ -81,5 +81,5 @@
81
81
  "publishConfig": {
82
82
  "access": "public"
83
83
  },
84
- "gitHead": "fa8891f69438a94440d4fbb73c8ed7dfff867311"
84
+ "gitHead": "6002d3517851d20c9b1c2ab5267d0c91ec74721b"
85
85
  }
@@ -6,26 +6,38 @@
6
6
  *
7
7
  */
8
8
 
9
- import { useState } from 'react';
9
+ import { useEffect, useState } from 'react';
10
10
  import { resizeObserver } from '@ndla/util';
11
11
 
12
12
  export const getMastheadHeight = (): number | undefined => {
13
- const masthead = document && document.getElementById('masthead');
14
- return masthead?.getBoundingClientRect().height;
13
+ if (typeof window !== 'undefined') {
14
+ const masthead = document.getElementById('masthead');
15
+ return masthead?.getBoundingClientRect().height;
16
+ }
15
17
  };
16
18
 
17
19
  export const useMastheadHeight = () => {
18
20
  const [height, setHeight] = useState<number>();
19
- const masthead = document && document.getElementById('masthead');
21
+ const [mounted, setMounted] = useState(false);
20
22
 
21
- const handleHeightChange = (el: HTMLElement) => {
22
- const newHeight = el.getBoundingClientRect().height;
23
- setHeight(newHeight);
24
- };
23
+ useEffect(() => {
24
+ setMounted(true);
25
+ }, []);
25
26
 
26
- if (masthead) {
27
- resizeObserver(masthead, handleHeightChange);
28
- }
27
+ useEffect(() => {
28
+ if (mounted) {
29
+ const masthead = document.getElementById('masthead');
30
+
31
+ const handleHeightChange = (el: HTMLElement) => {
32
+ const newHeight = el.getBoundingClientRect().height;
33
+ setHeight(newHeight);
34
+ };
35
+
36
+ if (masthead) {
37
+ resizeObserver(masthead, handleHeightChange);
38
+ }
39
+ }
40
+ }, [mounted]);
29
41
 
30
42
  return {
31
43
  height,