-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from hypothesis/link-pattern
Add `hyp-link` pattern and `Link` component
- Loading branch information
Showing
14 changed files
with
183 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* @typedef {import('preact').ComponentChildren} Children | ||
* | ||
* @typedef LinkBaseProps | ||
* @prop {Children} children | ||
* @prop {string} [classes] - Additional CSS classes to apply | ||
* @prop {import('preact').Ref<HTMLAnchorElement>} [linkRef] - Optional ref for | ||
* the rendered anchor element | ||
*/ | ||
|
||
/** | ||
* @typedef {LinkBaseProps & import('preact').JSX.HTMLAttributes<HTMLAnchorElement>} LinkProps | ||
*/ | ||
|
||
/** | ||
* Style and add some attributes to an anchor (`<a>`) element | ||
* | ||
* @param {LinkProps} props | ||
*/ | ||
export function Link({ children, classes = '', linkRef, ...restProps }) { | ||
return ( | ||
<a | ||
className={classnames('Hyp-Link', classes)} | ||
ref={linkRef} | ||
rel="noopener noreferrer" | ||
{...restProps} | ||
> | ||
{children} | ||
</a> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { mount } from 'enzyme'; | ||
import { createRef } from 'preact'; | ||
|
||
import { Link } from '../Link'; | ||
|
||
describe('Link', () => { | ||
const createComponent = (props = {}) => { | ||
return mount( | ||
<Link href="http://www.example.com" {...props}> | ||
This is content inside of a Link | ||
</Link> | ||
); | ||
}; | ||
|
||
it('renders children with appropriate classnames', () => { | ||
const wrapper = createComponent(); | ||
|
||
assert.isTrue(wrapper.find('a').first().hasClass('Hyp-Link')); | ||
}); | ||
|
||
it('applies extra classes', () => { | ||
const wrapper = createComponent({ classes: 'foo bar' }); | ||
assert.deepEqual( | ||
[...wrapper.find(`a.Hyp-Link.foo.bar`).getDOMNode().classList.values()], | ||
['Hyp-Link', 'foo', 'bar'] | ||
); | ||
}); | ||
|
||
it('passes along a `ref` to the `a` element through `linkRef`', () => { | ||
const linkRef = createRef(); | ||
createComponent({ linkRef }); | ||
|
||
assert.instanceOf(linkRef.current, Node); | ||
}); | ||
|
||
it('adds common `rel` attributes', () => { | ||
const wrapper = createComponent(); | ||
assert.equal( | ||
wrapper.find('a').first().getDOMNode().getAttribute('rel'), | ||
'noopener noreferrer' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Link } from '../../../'; | ||
import Library from '../Library'; | ||
|
||
export default function LinkComponents() { | ||
return ( | ||
<Library.Page title="Links"> | ||
<Library.Pattern title="Link"> | ||
<p> | ||
<code>Link</code> components provide some common styling and attribute | ||
for anchor (<code>a</code>) elements. | ||
</p> | ||
<Library.Example> | ||
<Library.Demo withSource> | ||
<Link href="https://www.example.com">A link to somewhere</Link> | ||
</Library.Demo> | ||
</Library.Example> | ||
</Library.Pattern> | ||
</Library.Page> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Library from '../Library'; | ||
|
||
export default function LinkPatterns() { | ||
return ( | ||
<Library.Page title="Links"> | ||
<Library.Pattern title="Link"> | ||
<p> | ||
The link pattern for <code>{'<a>'}</code> tags uses brand red and no | ||
underline, as well as a rounded keyboard focus ring. | ||
</p> | ||
<Library.Example> | ||
<Library.Demo withSource> | ||
<a className="hyp-link" href="http://www.example.com"> | ||
This is a link | ||
</a> | ||
<a className="hyp-link" href="http://www.example.com"> | ||
This is another link | ||
</a> | ||
</Library.Demo> | ||
</Library.Example> | ||
</Library.Pattern> | ||
</Library.Page> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@use '../mixins/patterns/links'; | ||
|
||
.Hyp-Link { | ||
@include links.link; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@use '../../variables' as var; | ||
|
||
@use '../focus'; | ||
|
||
$-border-radius: var.$border-radius; | ||
$-color-link: var.$color-link; | ||
$-color-link--hover: var.$color-link--hover; | ||
|
||
@mixin link { | ||
@include focus.outline-on-keyboard-focus; | ||
// Ensure the tag has width in order for :focus styling to render correctly. | ||
display: inline-block; | ||
color: $-color-link; | ||
text-decoration: none; | ||
// Give links a radius to allow :focus styling to appear similar to that of buttons | ||
border-radius: $-border-radius; | ||
|
||
&:active, | ||
&:focus { | ||
text-decoration: none; | ||
} | ||
|
||
&:hover { | ||
text-decoration: none; | ||
color: $-color-link--hover; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@use '../mixins/patterns/links'; | ||
|
||
.hyp-link { | ||
@include links.link; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
@use 'containers'; | ||
@use 'forms'; | ||
@use 'links'; | ||
@use 'panels'; | ||
@use 'spinners'; | ||
@use 'tables'; | ||
|