@ndlib/component-library 1.0.5 → 1.0.7
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/dist/components/composites/Card/Card.stories.js +8 -0
- package/dist/components/composites/Card/index.d.ts +1 -1
- package/dist/components/composites/Card/index.js +19 -4
- package/dist/components/elements/text/ReadMore/index.js +18 -13
- package/dist/utils/hooks/useManualRerender.d.ts +3 -0
- package/dist/utils/hooks/useManualRerender.js +18 -0
- package/package.json +1 -1
|
@@ -22,6 +22,14 @@ const headlinedCards = [
|
|
|
22
22
|
width: '500px',
|
|
23
23
|
},
|
|
24
24
|
},
|
|
25
|
+
{
|
|
26
|
+
headline: 'Paws, Hooves, Fins & Feathers',
|
|
27
|
+
image: 'https://strapi-prod-library-website-contentbucket52d4b12c-1whgwwl6746tz.s3.us-east-1.amazonaws.com/RBSC_Spring2020_Rep_2_a88660826b.jpg',
|
|
28
|
+
size: CARD_SIZE.SM,
|
|
29
|
+
sx: {
|
|
30
|
+
width: '500px',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
25
33
|
{
|
|
26
34
|
headline: 'Tracy C. Bergstrom, director of the Specialized Collection Services Program',
|
|
27
35
|
image: 'https://strapi-prod-library-website-contentbucket52d4b12c-1whgwwl6746tz.s3.us-east-1.amazonaws.com/Tracy_News_218x275_1dd180dc80.jpg',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import React, { useEffect, useState } from 'react';
|
|
2
3
|
import { HEADING_SIZE, Heading } from '../../elements/text/Heading';
|
|
3
4
|
import { ReadMore } from '../../elements/text/ReadMore';
|
|
4
5
|
import { useTheme } from '../../../theme';
|
|
@@ -7,7 +8,6 @@ import { Box } from '../../elements/layout/Box';
|
|
|
7
8
|
import { Icon } from '../../elements/Icon';
|
|
8
9
|
import { Column, FONT, FONT_SIZE, Row, TYPOGRAPHY_TYPE } from '../../..';
|
|
9
10
|
import { COLOR } from '../../../theme/colors';
|
|
10
|
-
import React, { useEffect, useState } from 'react';
|
|
11
11
|
import { KEY_CODES } from '../../../utils/misc';
|
|
12
12
|
import { useHover } from '../../../utils/hooks/useHover';
|
|
13
13
|
export var CARD_LAYOUT;
|
|
@@ -58,11 +58,26 @@ export const Card = ({ size, displayDate, headline, image, alt, layout, onClick,
|
|
|
58
58
|
const isVertical = !layout || layout === CARD_LAYOUT.VERTICAL;
|
|
59
59
|
const { isHovered, anchorElementProps } = useHover();
|
|
60
60
|
const [activeBackground, setActiveBackground] = useState(isHovered ? COLOR.ND_SKY_BLUE : COLOR.BACKGROUND);
|
|
61
|
-
const
|
|
62
|
-
const imageWidth = imageWidthProp || (isVertical ? '100%' : 'auto');
|
|
61
|
+
const [objectFit, setObjectFit] = useState('scale-down');
|
|
63
62
|
useEffect(() => {
|
|
64
63
|
setActiveBackground(isHovered ? COLOR.ND_SKY_BLUE : COLOR.BACKGROUND);
|
|
65
64
|
}, [isHovered]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (image) {
|
|
67
|
+
const img = new Image();
|
|
68
|
+
img.onload = () => {
|
|
69
|
+
if (img.width > img.height) {
|
|
70
|
+
setObjectFit('contain');
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
setObjectFit('scale-down');
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
img.src = image;
|
|
77
|
+
}
|
|
78
|
+
}, [image]);
|
|
79
|
+
const imageHeight = imageHeightProp || (isVertical ? 'auto' : '100%');
|
|
80
|
+
const imageWidth = imageWidthProp || (isVertical ? '100%' : 'auto');
|
|
66
81
|
const contentPaddingY = isVertical ? contentPaddingX : 2;
|
|
67
82
|
const typography = size === CARD_SIZE.SM
|
|
68
83
|
? TYPOGRAPHY_TYPE.HEADLINE_SMALL
|
|
@@ -90,7 +105,7 @@ export const Card = ({ size, displayDate, headline, image, alt, layout, onClick,
|
|
|
90
105
|
} }, { children: _jsx("img", { src: image, alt: alt, style: {
|
|
91
106
|
width: imageWidth,
|
|
92
107
|
height: imageHeight,
|
|
93
|
-
objectFit:
|
|
108
|
+
objectFit: objectFit,
|
|
94
109
|
} }) }))), displayDate && _jsx(DateDisplay, { date: displayDate }), heading && (_jsxs(Row, Object.assign({ sx: Object.assign({ bg: COLOR.PRIMARY, color: COLOR.WHITE, width: '100%', px: contentPaddingX, py: 3, justifyContent: 'space-between', alignItems: 'center' }, headingStyles) }, { children: [_jsxs(Row, { children: [headingIcon && (_jsx(Icon, { icon: headingIcon, size: FONT_SIZE.LG, color: COLOR.WHITE, sx: { mr: 2 } })), _jsx(Heading, Object.assign({ size: HEADING_SIZE.SM, sx: {
|
|
95
110
|
color: COLOR.WHITE,
|
|
96
111
|
mt: 0,
|
|
@@ -10,12 +10,13 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
12
|
import { jsx as _jsx, jsxs as _jsxs } from "theme-ui/jsx-runtime";
|
|
13
|
-
import React, { useState } from 'react';
|
|
13
|
+
import React, { useState, useEffect } from 'react';
|
|
14
14
|
import { useTheme } from '../../../../theme';
|
|
15
15
|
import { COLOR, colors } from '../../../../theme/colors';
|
|
16
16
|
import { getTypographyStyles, } from '../../../../theme/typography';
|
|
17
17
|
import { useMediaQuery } from '../../../providers/media';
|
|
18
18
|
import { multiplyRemSize } from '../../../../utils/misc';
|
|
19
|
+
import { useManualRerender } from '../../../../utils/hooks/useManualRerender';
|
|
19
20
|
const ReadMoreLink = (props) => {
|
|
20
21
|
const bg = colors[props.bg || COLOR.BACKGROUND];
|
|
21
22
|
const color = colors[props.color || COLOR.ND_BLUE_LIGHT];
|
|
@@ -44,22 +45,23 @@ export const useLinesHeight = (args) => {
|
|
|
44
45
|
});
|
|
45
46
|
};
|
|
46
47
|
export const ReadMore = (_a) => {
|
|
48
|
+
var _b, _c;
|
|
47
49
|
var { typography, sx, role, lines, fixedHeight, children } = _a, rest = __rest(_a, ["typography", "sx", "role", "lines", "fixedHeight", "children"]);
|
|
48
50
|
const ref = React.useRef(null);
|
|
49
51
|
const [showEllipsis, setShowEllipsis] = useState(true);
|
|
52
|
+
const forceRerender = useManualRerender();
|
|
50
53
|
const wrapperHeight = useLinesHeight({ typography, lines });
|
|
51
54
|
const { screenSize } = useMediaQuery();
|
|
52
55
|
const { color: _color, bg: _bg } = sx || {};
|
|
53
56
|
const color = Array.isArray(_color) ? _color[0] : _color;
|
|
54
57
|
const bg = Array.isArray(_bg) ? _bg[0] : _bg;
|
|
55
|
-
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
forceRerender({
|
|
60
|
+
delay: [100, 500, 2000],
|
|
61
|
+
});
|
|
62
|
+
}, []);
|
|
63
|
+
useEffect(() => {
|
|
56
64
|
if (ref.current) {
|
|
57
|
-
if (fixedHeight) {
|
|
58
|
-
ref.current.style.height = wrapperHeight;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
ref.current.style.maxHeight = wrapperHeight;
|
|
62
|
-
}
|
|
63
65
|
const isOverflowing = ref.current.scrollHeight > ref.current.clientHeight;
|
|
64
66
|
if (isOverflowing) {
|
|
65
67
|
setShowEllipsis(true);
|
|
@@ -68,9 +70,12 @@ export const ReadMore = (_a) => {
|
|
|
68
70
|
setShowEllipsis(false);
|
|
69
71
|
}
|
|
70
72
|
}
|
|
71
|
-
}, [
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
}, [
|
|
74
|
+
children,
|
|
75
|
+
wrapperHeight,
|
|
76
|
+
screenSize,
|
|
77
|
+
(_b = ref === null || ref === void 0 ? void 0 : ref.current) === null || _b === void 0 ? void 0 : _b.scrollHeight,
|
|
78
|
+
(_c = ref === null || ref === void 0 ? void 0 : ref.current) === null || _c === void 0 ? void 0 : _c.clientHeight,
|
|
79
|
+
]);
|
|
80
|
+
return (_jsxs("div", Object.assign({ role: role || 'paragraph', ref: ref, sx: Object.assign(Object.assign({ maxHeight: wrapperHeight, overflow: 'hidden', position: 'relative', height: fixedHeight ? wrapperHeight : 'auto' }, getTypographyStyles(typography)), sx) }, rest, { children: [children, showEllipsis && _jsx(ReadMoreLink, { bg: bg, color: color })] })));
|
|
76
81
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
export const useManualRerender = () => {
|
|
3
|
+
const [renderCount, updateRenderCount] = useState(0);
|
|
4
|
+
return useCallback((params) => {
|
|
5
|
+
const { delay } = params || {};
|
|
6
|
+
if (delay) {
|
|
7
|
+
const delayArray = Array.isArray(delay) ? delay : [delay];
|
|
8
|
+
for (const timeout of delayArray) {
|
|
9
|
+
setTimeout(() => {
|
|
10
|
+
updateRenderCount((renderCount) => renderCount + 1);
|
|
11
|
+
}, timeout);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
updateRenderCount(renderCount + 1);
|
|
16
|
+
}
|
|
17
|
+
}, [renderCount, updateRenderCount]);
|
|
18
|
+
};
|