Skip to content

Commit

Permalink
Merge pull request #1256 from statisticsnorway/add-ref-to-link
Browse files Browse the repository at this point in the history
Added ref to link
  • Loading branch information
michaelpande authored Sep 2, 2024
2 parents edb6325 + 8f37c44 commit f6a9ad6
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions src/components/Link/index.jsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
import React from 'react'
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'

const Link = ({
ariaLabel,
children,
className,
href,
icon,
isExternal = false,
linkType,
negative = false,
tabIndex,
title,
onClick = () => {},
standAlone,
}) => {
const classNames = `ssb-link${linkType ? ` ${linkType}` : ''}${standAlone ? ' stand-alone' : ''}
${negative ? ' negative' : ''}${icon ? ' with-icon' : ''}${className ? ` ${className}` : ''}`
const Link = forwardRef(
(
{
ariaLabel,
children,
className,
href,
icon,
isExternal = false,
linkType,
negative = false,
tabIndex,
title,
onClick = () => {},
standAlone,
},
ref
) => {
const classNames = `ssb-link${linkType ? ` ${linkType}` : ''}${standAlone ? ' stand-alone' : ''}
${negative ? ' negative' : ''}${icon ? ' with-icon' : ''}${className ? ` ${className}` : ''}`

return (
// eslint-disable-next-line react/jsx-no-target-blank
<a
className={classNames.replace(/\s\s+/g, ' ').trim()}
href={href}
target={isExternal ? '_blank' : undefined}
rel={isExternal ? 'noopener noreferrer' : undefined}
tabIndex={tabIndex}
aria-label={ariaLabel}
title={title}
onClick={onClick}
>
{icon && <div className='icon-wrapper'>{icon}</div>}
{children && <span className='link-text'>{children}</span>}
</a>
)
}
return (
// eslint-disable-next-line react/jsx-no-target-blank
<a
className={classNames.replace(/\s\s+/g, ' ').trim()}
href={href}
target={isExternal ? '_blank' : undefined}
rel={isExternal ? 'noopener noreferrer' : undefined}
tabIndex={tabIndex}
aria-label={ariaLabel}
title={title}
onClick={onClick}
ref={ref}
>
{icon && <div className='icon-wrapper'>{icon}</div>}
{children && <span className='link-text'>{children}</span>}
</a>
)
}
)

Link.propTypes = {
ariaLabel: PropTypes.string,
Expand Down

0 comments on commit f6a9ad6

Please sign in to comment.