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

[PLAY-1782] Link Kit Target Prop #4195

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions playbook/app/pb_kits/playbook/pb_link/_link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ type LinkProps = {
icon?: string,
iconRight?: string,
id?: string,
newWindow?: boolean,
tabIndex?: number,
tag?: 'a' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'span' | 'div',
target?: string,
text?: string,
underline?: boolean,
} & GlobalProps
Expand All @@ -37,7 +40,10 @@ const Link = (props: LinkProps): React.ReactElement => {
icon = '',
iconRight = '',
id = '',
newWindow = false,
tabIndex,
tag = 'a',
target = '',
text = '',
underline = false,
} = props
Expand All @@ -52,6 +58,16 @@ const Link = (props: LinkProps): React.ReactElement => {
)
const Tag = tag as keyof JSX.IntrinsicElements

const getTargetAttribute = () => {
if (target && href) {
return target
} else if (newWindow) {
return '_blank'
}

return undefined
}

const renderContent = () => (
<>
{icon && (
Expand Down Expand Up @@ -87,6 +103,9 @@ const Link = (props: LinkProps): React.ReactElement => {
<a
{...commonProps}
href={href}
rel={target !== 'child' ? 'noreferrer' : undefined}
tabIndex={tabIndex}
target={getTargetAttribute()}
>
{renderContent()}
</a>
Expand All @@ -96,6 +115,9 @@ const Link = (props: LinkProps): React.ReactElement => {
<a
{...commonProps}
href={href}
rel={target !== 'child' ? 'noreferrer' : undefined}
tabIndex={tabIndex}
target={getTargetAttribute()}
>
<Tag>{renderContent()}</Tag>
</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div>
<%= pb_rails("link", props: {
href: "http://google.com",
new_window: true,
target: "blank",
nidaqg marked this conversation as resolved.
Show resolved Hide resolved
text: "Open In New Window"
}) %>
</div>

<div>
<%= pb_rails("link", props: {
href: "https://playbook.powerapp.cloud/",
target: "child",
text: "Open In Child Tab",
}) %>
</div>
29 changes: 29 additions & 0 deletions playbook/app/pb_kits/playbook/pb_link/docs/_link_target.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import { Link } from 'playbook-ui'

const LinkTarget = (props) => (
<div>
<div>
<Link
aria={{ label: 'Link to Google in new window' }}
href="https://google.com"
tabIndex={0}
target='blank'
text="Open In New Window"
{...props}
/>
</div>
<div>
<Link
aria={{ label: 'Link to Playbook in a child tab' }}
href="https://playbook.powerapp.cloud/"
tabIndex={0}
target='child'
text="Open In Child Tab"
{...props}
/>
</div>
</div>
)

export default LinkTarget
8 changes: 5 additions & 3 deletions playbook/app/pb_kits/playbook/pb_link/docs/example.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
examples:

rails:
- link_color: Color
- link_underline: Underline
- link_icon: Icon
- link_disabled: Disabled
- link_tag: Tag


- link_target: Target


react:
- link_color: Color
- link_underline: Underline
- link_icon: Icon
- link_disabled: Disabled
- link_tag: Tag
- link_target: Target
3 changes: 2 additions & 1 deletion playbook/app/pb_kits/playbook/pb_link/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { default as LinkColor } from './_link_color.jsx'
export { default as LinkUnderline } from './_link_underline.jsx'
export { default as LinkIcon } from './_link_icon.jsx'
export { default as LinkDisabled } from './_link_disabled.jsx'
export { default as LinkTag } from './_link_tag.jsx'
export { default as LinkTag } from './_link_tag.jsx'
export { default as LinkTarget } from './_link_target.jsx'
2 changes: 1 addition & 1 deletion playbook/app/pb_kits/playbook/pb_link/link.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% end %>

<% if object.tag == "a" %>
<%= pb_content_tag(object.tag, { href: object.href }) do %>
<%= pb_content_tag(object.tag, { href: object.href, target: object.target }) do %>
<%= link_content.call %>
<% end %>
<% else %>
Expand Down
12 changes: 12 additions & 0 deletions playbook/app/pb_kits/playbook/pb_link/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ class Link < ::Playbook::KitBase
prop :href
prop :icon
prop :icon_right
prop :new_window, type: Playbook::Props::Boolean,
default: false
prop :tabindex
prop :tag, type: Playbook::Props::Enum,
values: %w[a h1 h2 h3 h4 h5 h6 p span div],
default: "a"
prop :target
prop :text
prop :underline, type: Playbook::Props::Boolean,
default: false
Expand All @@ -26,6 +30,14 @@ def content
text
end

def target_attribute
if target && href
target
elsif new_window
"_blank"
end
end

private

def color_class
Expand Down
30 changes: 30 additions & 0 deletions playbook/app/pb_kits/playbook/pb_link/link.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,33 @@ test('adds icon right', () => {
const icon = kit.querySelector('.pb_icon_kit')
expect(icon).toBeInTheDocument();
})

test('should render target prop', () => {
render(
<Link
data={{ testid: 'target-test' }}
href="https://playbook.powerapp.cloud/"
target="blank"
/>
)

const kit = screen.getByTestId('target-test')

expect(kit).toHaveAttribute('target', 'blank')
})


test('should render child target prop', () => {
render(
<Link
data={{ testid: 'target-test' }}
href="https://playbook.powerapp.cloud/"
tabIndex={0}
target="child"
/>
)

const kit = screen.getByTestId('target-test')

expect(kit).toHaveAttribute('target', 'child')
})
Loading