-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add usa-link
component
#8
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I was able to test both variants successfully. As well as tel
and mailto
without any issues.
I was also able to override an individual link via a CSS class.
<style>
:root {
--theme-link-color: orange;
}
.james {
--theme-link-color: green;
}
</style>
<p>
<usa-link href="http://designsystem.digital.gov">
It's dangerous to go alone. Here, take this.
</usa-link>
</p>
<p>
<usa-link class="james">
<a href="https://designsystem.digital.gov">It's dangerous to go alone. Here, take this.</a>
</usa-link>
</p>
<p>
<usa-link>
<a href="https://google.com">A test link</a>
</usa-link>
</p>
: html`<a class="usa-link" href="${this.href}"><slot></slot></a>`; | ||
} | ||
|
||
static styles = css` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to establish a pattern on where styles should live.
Placing them above render()
feels familiar to setting a <link rel="stylesheet">
above HTML markup. Though I can see the benefits of having styles after for convenience while developing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any benefit of having css in JS like this over having a separate styles.css
sheet and pulling it in via unsafeCSS
?
I see a benefit of being able to write CSS in a CSS file and keeping index.js
as JS focused as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I don't have any objections to keeping styles in separate files alongside the component code. Definitely helps maintainability. I'll factor them out here.
return this.hasLinkChild() | ||
? html`<a class="usa-link" href="${this.href}" | ||
>${this.slottedChildren}</a | ||
>` | ||
: html`<a class="usa-link" href="${this.href}"><slot></slot></a>`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could improve readability and make it easier to update if we separated these templates out. Like in the example here - Composing templates – Lit
linkChildTemplate() {
return html`<a class="usa-link" href="${this.href}">${this.slottedChildren}</a>`
}
linkSlotTemplate() {
return html`<a class="usa-link" href="${this.href}"><slot></slot></a>`
}
return this.hasLinkChild()
? this.linkChildTemplate()
: this.linkSlotTemplate()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm personally a fan of this. I took a similar approach with the card component.
@mejiaj Thanks for your thoughtful feedback! I was totally onboard with your suggestions, and implemented them. I also added some JSDoc comments, along the lines of what's described here (not necessarily suggesting incorporating that tool unless it makes sense, but I've seen a lot of components doing docs with this format). |
Note
This is a basic implementation of this component which is not necessarily where it will need to be for alpha status. This relates to #9 but the work in this PR does not close that issue.
This is a basic implementation of a
usa-link
component. It supports both JS-required and a progressively-enhanced version. So a user can just include the component, like:which will render a vanilla HTML link to the shadow DOM with the right styling (additional functionality TK). Alternatively, a user can include their own link which will render as the original HTML before JS runs (or if it doesn't run at all), and will get additional functionality if/when the script executes.
In practice, that looks like this. This shows the case when JS is disabled. The first text on the page is the JS-required version, which renders as unclickable text. The second
![image](https://private-user-images.githubusercontent.com/6290/338234475-7abc677b-2d82-4840-8b42-0465b56bd48e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk4MDgzNzEsIm5iZiI6MTczOTgwODA3MSwicGF0aCI6Ii82MjkwLzMzODIzNDQ3NS03YWJjNjc3Yi0yZDgyLTQ4NDAtOGI0Mi0wNDY1YjU2YmQ0OGUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTdUMTYwMTExWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MjYwMjI4N2Q1OTM5ZDNiNTM5Y2QwNDc2ZGQ0ODc2MjQ2MmFlYzkzZGY4MDM0OGRlYjk4Y2E3NTFhZjlkNWY4NSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.REq0BfQGZCtPA63izNgAydHM_LZxr7ws-o4CXJdyjaQ)
usa-link
, with the plain HTML link as a child, still renders:This is what the DOM looks like for these two versions of the component when JS doesn't run:
![image](https://private-user-images.githubusercontent.com/6290/338234678-202330a9-f1b2-40ef-be17-776f8179f808.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk4MDgzNzEsIm5iZiI6MTczOTgwODA3MSwicGF0aCI6Ii82MjkwLzMzODIzNDY3OC0yMDIzMzBhOS1mMWIyLTQwZWYtYmUxNy03NzZmODE3OWY4MDgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTdUMTYwMTExWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjI2NjE5NjdiZDI0ZTRhMGI0MGEwMDEwYWQ3OTI1NWQ1Yjk3M2Y0ODljZWVhYTMxYjRiNzM1MjA4MWNkMTZlMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.eX1VF1BJ9uXcBAi4MQXeDJh_dg22zpbplIHFhhSg61Y)
When JS does run, both versions are equivalent with the same styling applied to both.