Skip to content

Commit

Permalink
fix: correctly delivery visuals and mouse interactions for litAnchor …
Browse files Browse the repository at this point in the history
…and extensions
  • Loading branch information
Westbrook committed Mar 16, 2021
1 parent a5a204f commit 0ae889a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 22 deletions.
5 changes: 5 additions & 0 deletions packages/action-button/src/action-button.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ governing permissions and limitations under the License.
flex-shrink: 0;
}

#button {
position: absolute;
inset: 0;
}

#label {
flex-grow: var(--spectrum-actionbutton-label-flex-grow);
text-align: var(--spectrum-actionbutton-label-text-align);
Expand Down
12 changes: 12 additions & 0 deletions packages/action-button/stories/action-button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ wIconButton.story = {
name: 'w/ Icon button',
};

export const likeAnchor = (): TemplateResult => {
return html`
<sp-action-button
href="https://opensource.adobe.com/spectrum-web-components"
target="_blank"
>
<sp-icon-edit slot="icon"></sp-icon-edit>
Edit the Documentation Site
</sp-action-button>
`;
};

export const iconOnlyButton = (): TemplateResult => {
return html`
<sp-action-button label="Edit">
Expand Down
23 changes: 18 additions & 5 deletions packages/button/src/ButtonBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class ButtonBase extends LikeAnchor(

constructor() {
super();
this.proxyFocus = this.proxyFocus.bind(this);

this.addEventListener('click', this.handleClickCapture, {
capture: true,
Expand Down Expand Up @@ -102,11 +103,16 @@ export class ButtonBase extends LikeAnchor(
}
}

private proxyFocus(): void {
this.focus();
}

private shouldProxyClick(): boolean {
let handled = false;
if (this.anchorElement) {
this.anchorElement.click();
handled = true;
this.anchorElement.classList.remove('clicking');
} else if (this.type !== 'button') {
const proxy = document.createElement('button');
proxy.type = this.type;
Expand All @@ -118,6 +124,16 @@ export class ButtonBase extends LikeAnchor(
return handled;
}

public renderAnchor(): TemplateResult {
return html`
${this.buttonContent}
${super.renderAnchor({
id: 'button',
className: 'button anchor hidden',
})}
`;
}

protected renderButton(): TemplateResult {
return html`
${this.buttonContent}
Expand All @@ -126,11 +142,7 @@ export class ButtonBase extends LikeAnchor(

protected render(): TemplateResult {
return this.href && this.href.length > 0
? this.renderAnchor({
id: 'button',
className: 'button anchor',
anchorContent: this.buttonContent,
})
? this.renderAnchor()
: this.renderButton();
}

Expand Down Expand Up @@ -223,6 +235,7 @@ export class ButtonBase extends LikeAnchor(
}
}
if (this.anchorElement) {
this.anchorElement.addEventListener('focus', this.proxyFocus);
this.anchorElement.tabIndex = -1;
}
}
Expand Down
9 changes: 2 additions & 7 deletions packages/button/src/button-base.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ governing permissions and limitations under the License.
}

#button {
color: inherit;
text-decoration: inherit;
display: flex;
}

#button:focus {
outline: none;
position: absolute;
inset: 0;
}

:host:after {
Expand Down
10 changes: 0 additions & 10 deletions packages/menu/src/MenuItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ export class MenuItem extends ActionButton {
return content;
}

public renderAnchor(): TemplateResult {
return html`
${this.buttonContent}
${super.renderAnchor({
id: 'button',
className: 'button anchor hidden',
})}
`;
}

protected renderButton(): TemplateResult {
return html`
${this.buttonContent}
Expand Down
5 changes: 5 additions & 0 deletions packages/menu/src/menu-item.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ governing permissions and limitations under the License.
*/

@import './spectrum-menu-item.css';

#button {
position: absolute;
inset: 0;
}
14 changes: 14 additions & 0 deletions packages/menu/stories/menu-item.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ export const noWrap = (): TemplateResult => {
</sp-menu>
`;
};

export const href = (): TemplateResult => {
return html`
<sp-menu style="width: 150px;">
<sp-menu-item
href="https://opensource.adobe.com/spectrum-web-components"
target="_blank"
>
<sp-icon-edit slot="icon"></sp-icon-edit>
Edit the Documentation Site
</sp-menu-item>
</sp-menu>
`;
};

0 comments on commit 0ae889a

Please sign in to comment.