Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

feat(link): add icon-placement option #1009

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions src/components/link/link-story.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2021
* Copyright IBM Corp. 2019, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -16,7 +16,7 @@ import { boolean, select } from '@storybook/addon-knobs';
import Download16 from 'carbon-web-components/es/icons/download/16';
import textNullable from '../../../.storybook/knob-text-nullable';
import ifNonNull from '../../globals/directives/if-non-null';
import { LINK_SIZE } from './link';
import { LINK_SIZE, ICON_PLACEMENT } from './link';
import storyDocs from './link-story.mdx';

const sizes = {
Expand All @@ -25,6 +25,11 @@ const sizes = {
[`Large size (${LINK_SIZE.LARGE})`]: LINK_SIZE.LARGE,
};

const placementTypes = {
[`${ICON_PLACEMENT.LEFT}`]: ICON_PLACEMENT.LEFT,
[`${ICON_PLACEMENT.RIGHT}`]: ICON_PLACEMENT.RIGHT,
};

export const Default = args => {
const { disabled, download, href, hreflang, linkRole, ping, rel, size, target, type, onClick } = args?.['bx-link'] ?? {};
return html`
Expand All @@ -48,13 +53,15 @@ export const Default = args => {
Default.storyName = 'Default';

export const pairedWithIcon = args => {
const { disabled, download, href, hreflang, linkRole, ping, rel, size, target, type, onClick } = args?.['bx-link'] ?? {};
const { disabled, download, href, hreflang, iconPlacement, linkRole, ping, rel, size, target, type, onClick } =
args?.['bx-link'] ?? {};
return html`
<bx-link
?disabled="${disabled}"
download="${ifNonNull(download)}"
href="${ifNonNull(href)}"
hreflang="${ifNonNull(hreflang)}"
icon-placement="${iconPlacement}"
link-role="${ifNonNull(linkRole)}"
ping="${ifNonNull(ping)}"
rel="${ifNonNull(rel)}"
Expand All @@ -75,6 +82,7 @@ export default {
'bx-link': () => ({
disabled: boolean('Disabled (disabled)', false),
href: textNullable('Link href (href)', 'https://github.com/carbon-design-system/carbon-web-components'),
iconPlacement: select('Icon Position (icon-placement):', placementTypes, placementTypes[`${ICON_PLACEMENT.RIGHT}`]),
onClick: action('click'),
size: select('Link size (size)', sizes, null),
}),
Expand Down
18 changes: 17 additions & 1 deletion src/components/link/link.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright IBM Corp. 2019, 2021
// Copyright IBM Corp. 2019, 2022
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
Expand All @@ -20,4 +20,20 @@ $css--plex: true !default;
.#{$prefix}--link__icon[hidden] {
display: none;
}

&.#{$prefix}--link-with-icon__icon-left,
.#{$prefix}--link-with-icon.#{$prefix}--link-with-icon--inline-icon.#{$prefix}--link-with-icon__icon-left {
display: flex;
justify-content: flex-end;
flex-direction: row-reverse;

svg,
::slotted(svg[slot='icon']) {
align-self: start;
position: relative;
margin-left: 0;
margin-right: $carbon--spacing-03;
top: 1px;
}
}
}
33 changes: 32 additions & 1 deletion src/components/link/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2021
* Copyright IBM Corp. 2019, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -10,10 +10,13 @@
import { classMap } from 'lit-html/directives/class-map';
import { html, property, customElement, LitElement, query } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import { ICON_PLACEMENT } from '../../globals/defs';
import ifNonNull from '../../globals/directives/if-non-null';
import FocusMixin from '../../globals/mixins/focus';
import styles from './link.scss';

export { ICON_PLACEMENT };

const { prefix } = settings;

/**
Expand Down Expand Up @@ -149,6 +152,18 @@ class BXLink extends FocusMixin(LitElement) {
@property({ reflect: true })
hreflang!: string;

/**
* Icon placement(right (default) | left)
*/
@property({ attribute: 'icon-placement', reflect: true })
iconPlacement = ICON_PLACEMENT.RIGHT;

/**
* Positions the icon inline with text when `true`
*/
@property({ type: Boolean })
iconInline = true;

/**
* The a11y role for `<a>`.
*/
Expand Down Expand Up @@ -192,6 +207,22 @@ class BXLink extends FocusMixin(LitElement) {
});
}

updated() {
const { iconInline, iconPlacement, _linkNode: linkNode } = this;
if (linkNode) {
linkNode.classList.add(`${prefix}--link-with-icon`);
linkNode.classList.toggle(`${prefix}--link-with-icon__icon-${ICON_PLACEMENT.LEFT}`, iconPlacement === ICON_PLACEMENT.LEFT);
linkNode.classList.toggle(
`${prefix}--link-with-icon__icon-${ICON_PLACEMENT.RIGHT}`,
iconPlacement === ICON_PLACEMENT.RIGHT
);

if (iconInline && iconPlacement === ICON_PLACEMENT.RIGHT) {
linkNode.classList.add(`${prefix}--link-with-icon--inline-icon`);
}
}
}

render() {
const { disabled } = this;
return disabled ? this._renderDisabledLink() : this._renderLink();
Expand Down
23 changes: 23 additions & 0 deletions src/globals/defs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
*
* Copyright IBM Corp. 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* Icon Placement.
*/
export enum ICON_PLACEMENT {
/**
* left of footer copy
*/
LEFT = 'left',

/**
* right of footer copy
*/
RIGHT = 'right',
}