-
Notifications
You must be signed in to change notification settings - Fork 18
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 Tag component #4
Conversation
@@ -0,0 +1,3 @@ | |||
<strong class="govuk-tag <%= css_classes %>"> |
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.
For Apply we need to add a custom CSS class. Do we skip doing that for now?
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 think this would be worth while. On Publish we are using custom classes on tags
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.
Could follow a similar pattern to this https://github.com/DFE-Digital/govuk-components/blob/master/app/components/govuk_component/back_link.rb#L7-L8
app/components/tag/tag_component.rb
Outdated
end | ||
|
||
def css_classes | ||
@colour ? "govuk-tag--#{@colour}" : '' |
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.
Should we validate the colours?
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.
Or should we just allow passing in classes
like the nunjucks macro?
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.
The angle I took with the form builder was to provide arguments for the documented features of a component. Here's a relevant example for configuring input width; in this case as a user, I'd probably want to be able to call something like Tag::TagComponent.new(text: 'Danger', colour: 'red')
.
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.
There are mixed feelings on this. @adamsilver once tried doing something custom with a component in the Form builder that wasn't possible because you couldn't pass in custom classes
. I ended up submitting a patch upstream: x-govuk/govuk-form-builder#96
The design system does allow you to pass custom classes for most of its components, and I think it's to allow users to break encapsulation where it's necessary, such as defining new variants of a component that are not yet upstream.
I think it makes sense to allow not only passing in classes
but also providing smart arguments that interpret the specification in order to make components easy to use. So though it's more hard work, I think all 3 of these have their use-cases, and should be supported:
GovukComponent::Tag.new(text: 'Danger', colour: 'red')
GovukComponent::Tag.new(text: 'Danger', colour: 'danger')
GovukComponent::Tag.new(text: 'Danger', classes: 'govuk-tag--red')
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.
If anything, the 'danger'
semantic variant could be dropped, I think it's the least important.
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.
What makes the nunjuck macros great is that they also allow you to pass in additional html attributes and the fixability it brings. Heres some standards they use to determine the API for components. I think we should inherit this to help use keep this gem up to date when the govuk-frontend makes breaking changes.
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.
The Nunjucks macros are powerful and flexible but I'm really not a fan of passing in strings of HTML. Invariably, from experience, you just end up with HTML in a string in a block of JavaScript/Ruby in a HTML(-like) template.
expect(html).to eql(<<~html | ||
<strong class="govuk-tag govuk-tag--green"> | ||
Alert | ||
</strong> | ||
html | ||
) |
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.
This is different from the existing tests. I wonder if this makes it easier to check whether or not the HTML is correctly formed. For me this was easier to do then translating it into have_css
assertions like the previous tests.
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.
This is similar to Jest inline snapshots from the JavaScript world, can confirm it is a very efficient and sane way for testing small blocks of HTML or JSON.
One difference is that jest
(rspec
equivalent) gives you a button (jest --updateSnapshot
) to update all out of date snapshots, which makes refactoring tests en-masse very easy for banal changes.
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 tend to lean towards the have_css
approach because it's ignorant of whitespace, indentation etc.
expect(html).to have_css('strong', class: 'govuk-tag govuk-tag--green', text: 'Alert')
On smaller, simpler components matching the HTML exactly works well and is readable, this is fine by me
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 agree re: whitespace @peteryates. That's where having a tool do it for you comes in really handy.
(This exists but it doesn't look maintained https://github.com/yesmeck/rspec-snapshot)
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.
🤔 If we render the components on a page we could use jest to snapshot the component. We could go one step further and do some visual regression testing using jest-image-snapshot (developed by american express) https://www.npmjs.com/package/jest-image-snapshot
These are the params that govuk-frontend nunjuck macro accepts https://github.com/alphagov/govuk-frontend/blob/master/src/govuk/components/tag/tag.yaml#L1-L17 |
483a4fd
to
b45f905
Compare
@peteryates you don't need to (and shouldn't) capitalise the text, the CSS styling takes care of that. |
@adamsilver |
It's unnecessary and displayed in uppercase on the page anyway.
fe0335c
to
3202569
Compare
Quick PR to discuss some patterns and approaches.