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

feat(image): pass through all props of anchor element #274

Merged
merged 2 commits into from
Jun 12, 2020
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
11 changes: 11 additions & 0 deletions components/image/__tests__/browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ describe('Image Browser', () => {
const wrapper = mount(<Image.Browser />)
expect(() => wrapper.unmount()).not.toThrow()
})

it('anchor props should be passed through', () => {
const anchorRel = 'noreferrer'
const wrapper = mount(
<Image.Browser url={link} anchorProps={{ rel: anchorRel }}>
<Image src={url} />
</Image.Browser>,
)
const rel = wrapper.find('a').getDOMNode().getAttribute('rel')
expect(anchorRel).toEqual(anchorRel)
})
})
20 changes: 15 additions & 5 deletions components/image/image-browser.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import React, { useMemo } from 'react'
import Link from '../link'
import { Props as LinkProps } from '../link/link'
import useTheme from '../styles/use-theme'
import withDefaults from '../utils/with-defaults'
import ImageBrowserHttpsIcon from './image-browser-https-icon'
import { getBrowserColors, BrowserColors } from './styles'

type AnchorProps = Omit<React.AnchorHTMLAttributes<any>, keyof LinkProps>

interface Props {
title?: string
url?: string
showFullLink?: boolean
invert?: boolean
anchorProps?: AnchorProps
className?: string
}

const defaultProps = {
className: '',
showFullLink: false,
anchorProps: {} as AnchorProps,
invert: false,
}

Expand All @@ -42,12 +47,17 @@ const getTitle = (title: string, colors: BrowserColors) => (
</div>
)

const getAddressInput = (url: string, showFullLink: boolean, colors: BrowserColors) => (
const getAddressInput = (
url: string,
showFullLink: boolean,
colors: BrowserColors,
anchorProps: AnchorProps,
) => (
<div className="address-input">
<span className="https">
<ImageBrowserHttpsIcon />
</span>
<Link href={url} title={url} target="_blank">
<Link href={url} title={url} target="_blank" {...anchorProps}>
{showFullLink ? url : getHostFromUrl(url)}
</Link>
<style jsx>{`
Expand Down Expand Up @@ -94,16 +104,16 @@ const getAddressInput = (url: string, showFullLink: boolean, colors: BrowserColo

const ImageBrowser = React.forwardRef<HTMLDivElement, React.PropsWithChildren<ImageBrowserProps>>(
(
{ url, title, children, showFullLink, invert, className, ...props },
{ url, title, children, showFullLink, invert, anchorProps, className, ...props },
ref: React.Ref<HTMLDivElement>,
) => {
const theme = useTheme()
const colors = useMemo(() => getBrowserColors(invert, theme.palette), [invert, theme.palette])
const input = useMemo(() => {
if (url) return getAddressInput(url, showFullLink, colors)
if (url) return getAddressInput(url, showFullLink, colors, anchorProps)
if (title) return getTitle(title, colors)
return null
}, [url, showFullLink, title, colors])
}, [url, showFullLink, title, colors, anchorProps])

return (
<div className={`bowser ${className}`} ref={ref} {...props}>
Expand Down
3 changes: 2 additions & 1 deletion pages/en-us/components/image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Display image content.
desc="Add a browser style wrapper to the image."
scope={{ Image, Display, Code }}
code={`
<Image.Browser url="https://react.zeit-ui.co/en-us/guide/introduction" >
<Image.Browser url="https://react.zeit-ui.co/en-us/guide/introduction" anchorProps={{ rel: 'nofollow' }}>
<Image width="540" height="246" src="https://user-images.githubusercontent.com/11304944/76085431-fd036480-5fec-11ea-8412-9e581425344a.png" />
</Image.Browser>
`} />
Expand Down Expand Up @@ -77,6 +77,7 @@ Display image content.
| **url** | show url on browser address input | `string` | - | - |
| **showFullLink** | show full url | `boolean` | - | `false` |
| **invert** | invert colors | `boolean` | - | `false` |
| **anchorProps** | props of element `a` | `AnchorHTMLAttributes` | - | `{}` |
| ... | native props | `HTMLAttributes` | `'id', 'className', ...` | - |

</Attributes>
Expand Down
1 change: 1 addition & 0 deletions pages/zh-cn/components/image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const meta = {
| **url** | 在浏览器地址栏显示链接 | `string` | - | - |
| **showFullLink** | 显示完整的链接而非域名 | `boolean` | - | `false` |
| **invert** | 反转所有颜色 | `boolean` | - | `false` |
| **anchorProps** | 设置 `a` 的其他属性 | `AnchorHTMLAttributes` | - | `{}` |
| ... | 原生属性 | `HTMLAttributes` | `'id', 'className', ...` | - |

</Attributes>
Expand Down