Skip to content

Commit

Permalink
Merge pull request #976 from statisticsnorway/MIM-340_ref-picturecard
Browse files Browse the repository at this point in the history
Added forwardRef around PictureCard so it can be used with useRef
  • Loading branch information
omsaggau committed Feb 23, 2024
2 parents eb85ceb + e069bf1 commit 0a3291b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@statisticsnorway/ssb-component-library",
"version": "2.0.97",
"version": "2.0.98",
"description": "Component library for SSB (Statistics Norway)",
"main": "lib/bundle.js",
"scripts": {
Expand Down
35 changes: 30 additions & 5 deletions src/components/PictureCard/index.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, { useRef, useState, useEffect } from 'react';
import React, { forwardRef, useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { ArrowRight, ArrowRightCircle } from 'react-feather';

export function useHover() {
const [value, setValue] = useState(false);

const hoverRef = useRef(null);

useEffect(
// eslint-disable-next-line consistent-return
() => {
Expand All @@ -29,14 +28,22 @@ export function useHover() {
return [hoverRef, value];
}

const PictureCard = ({ className, imageSrc, altText, link, onClick, orientation, title, type }) => {
const PictureCard = forwardRef(({
className, imageSrc, altText, link, onClick, orientation, title, type,
}, ref) => {
const [hoverRef, hovered] = useHover();
return (
<a
className={`ssb-picture-card ${orientation} ${className || ''}`}
href={link}
onClick={onClick}
ref={hoverRef}
ref={element => {
// Using ref for multiple purposes, so need to set it manually
if (typeof ref === 'function') ref(element);
// eslint-disable-next-line no-param-reassign
else if (ref) { ref.current = element; }
hoverRef.current = element;
}}
>
<div className="image-background">
<img src={imageSrc} alt={altText} />
Expand All @@ -50,7 +57,7 @@ const PictureCard = ({ className, imageSrc, altText, link, onClick, orientation,
</div>
</a>
);
};
});

PictureCard.defaultProps = {
onClick: () => {
Expand All @@ -70,3 +77,21 @@ PictureCard.propTypes = {
};

export default PictureCard;

/*
ref={element => {
// Using ref for multiple purposes, so need to set it manually
if (typeof ref === 'function') ref(element);
// eslint-disable-next-line no-param-reassign
else if (ref) { ref.current = element; }
hoverRef.current = element;
}}
The ref is set in this way so it can be both used by a parent component with useRef and the internal hoverRef hook.
The 'if (typeof ref === 'function')' is used so a parent can use the two commonly used syntaxes for setting refs
Both of these will now work:
<PictureCard ref={(element) => (cards.current[index] = element)}
<PictureCard ref={myRef}
The first example is the most used way to set refs on components in .map functions.
*/

0 comments on commit 0a3291b

Please sign in to comment.