Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added forwardRef around PictureCard so it can be used with useRef #976

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
*/