-
Notifications
You must be signed in to change notification settings - Fork 12
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
va-language-toggle: add component #1386
base: main
Are you sure you want to change the base?
Changes from 7 commits
6df795d
60b523e
2c71555
440173b
d062e63
18f0492
1b91400
6f927bb
61001d8
c24f1cf
92eed55
27bd031
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { Fragment } from 'react'; | ||
import { getWebComponentDocs, StoryDocs, propStructure } from './wc-helpers'; | ||
import { VaLanguageToggle } from '@department-of-veterans-affairs/web-components/react-bindings'; | ||
|
||
const languageToggleDocs = getWebComponentDocs('va-language-toggle'); | ||
|
||
export default { | ||
title: 'Components/Language Toggle', | ||
id: 'components/va-language-toggle', | ||
parameters: { | ||
componentSubtitle: 'va-language-toggle web component', | ||
docs: { | ||
page: () => <StoryDocs storyDefault={Default} data={languageToggleDocs} />, | ||
}, | ||
}, | ||
}; | ||
|
||
const url = new URL(window.parent.location.href); | ||
url.searchParams.set('path', '/docs/components-va-language-toggle--docs'); | ||
|
||
const defaultArgs = { | ||
enHref: url.href, | ||
esHref: url.href, | ||
tlHref: url.href, | ||
} | ||
|
||
const Template = ({ enHref, esHref, tlHref }) => { | ||
let lang = sessionStorage.getItem('va-language-toggle-lang') ?? 'en'; | ||
function handleLanguageToggle(e) { | ||
const { language } = e.detail; | ||
sessionStorage.setItem('va-language-toggle-lang', language) | ||
} | ||
|
||
return ( | ||
<VaLanguageToggle | ||
language={lang} | ||
enHref={enHref} | ||
esHref={esHref} | ||
tlHref={tlHref} | ||
onVaLanguageToggle={handleLanguageToggle} | ||
/> | ||
); | ||
}; | ||
|
||
const WithRouterLinksTemplate = ({ enHref, esHref, tlHref }) => { | ||
|
||
function handleLanguageToggle(e) { | ||
console.log(`the language has been toggled to ${e.detail.language}`); | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<div>This example illustrates how to use the component with a router. When <code>router-links</code> is | ||
set to <code>true</code>, clicking on a link will not navigate to a new page (i.e. <code>event.preventDefault()</code> is called). | ||
By capturing the <code>language-toggle</code> event page content can be updated as needed to reflect the selected language. | ||
</div> | ||
<VaLanguageToggle | ||
enHref={enHref} | ||
esHref={esHref} | ||
tlHref={tlHref} | ||
routerLinks={true} | ||
onVaLanguageToggle={handleLanguageToggle} | ||
/> | ||
</Fragment> | ||
) | ||
} | ||
|
||
export const Default = Template.bind(null); | ||
Default.args = { | ||
...defaultArgs, | ||
}; | ||
Default.argTypes = propStructure(languageToggleDocs); | ||
|
||
export const WithRouterLinks = WithRouterLinksTemplate.bind(null); | ||
WithRouterLinks.args = { ...defaultArgs } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { newE2EPage } from "@stencil/core/testing"; | ||
import { axeCheck } from "../../../testing/test-helpers"; | ||
|
||
describe('va-language-toggle', () => { | ||
const enHref = "/resources/the-pact-act-and-your-va-benefits/"; | ||
const esHref = "/resources/the-pact-act-and-your-va-benefits-esp/" | ||
const tlHref = "/resources/the-pact-act-and-your-va-benefits-tl/" | ||
|
||
it('renders', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
const element = await page.find('va-language-toggle'); | ||
expect(element).toHaveClass('hydrated'); | ||
}); | ||
|
||
it('English is the default language', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
const anchor = await page.find('va-language-toggle >>> a'); | ||
expect(anchor).toHaveClass('is-current-lang'); | ||
}); | ||
|
||
it('only renders links for those languages with supplied hrefs', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle en-href="${enHref}" es-href="${esHref}" />`); | ||
const anchors = await page.findAll('va-language-toggle >>> a'); | ||
expect(anchors).toHaveLength(2); | ||
}) | ||
|
||
it('if language prop is set the matching language is bolded', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle language="es" en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
const [_, anchor] = await page.findAll('va-language-toggle >>> a'); | ||
expect(anchor).toHaveClass('is-current-lang'); | ||
}); | ||
|
||
it('if router-links is set, clicking an anchor tag does not result in page navigation', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle router-links="true" en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
const [startUrl] = page.url().split('?'); | ||
const [_, anchor] = await page.findAll('va-language-toggle >>> a'); | ||
await anchor.click(); | ||
const [endUrl] = page.url().split('?'); | ||
expect(startUrl).toEqual(endUrl); | ||
}); | ||
|
||
it('fires a language-toggle event when a link is clicked', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
const toggleSpy = await page.spyOnEvent('vaLanguageToggle'); | ||
const [_, anchor] = await page.findAll('va-language-toggle >>> a'); | ||
await anchor.click(); | ||
expect(toggleSpy).toHaveReceivedEvent(); | ||
}); | ||
|
||
it('fires a component-analytics event when a link is clicked', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
const toggleSpy = await page.spyOnEvent('component-library-analytics'); | ||
const anchor = await page.find('va-language-toggle >>> a'); | ||
await anchor.click(); | ||
expect(toggleSpy).toHaveReceivedEvent(); | ||
}); | ||
|
||
it('passes an aXe check', async () => { | ||
const page = await newE2EPage(); | ||
await page.setContent(`<va-language-toggle en-href="${enHref}" es-href="${esHref}" tl-href="${tlHref}" />`); | ||
await axeCheck(page); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@import '../../mixins/focusable.css'; | ||
|
||
:host { | ||
display: inline-block; | ||
margin: 32px 0 24px 0; | ||
border-bottom: 1px solid var(--vads-color-base-dark); | ||
|
||
span { | ||
margin: 0 4px 0 4px; | ||
color: var(--vads-color-base-dark); | ||
} | ||
|
||
a { | ||
&.is-current-lang { | ||
font-weight: var(--font-weight-bold); | ||
text-decoration: none; | ||
color: var(--vads-color-base); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a Figma link that can be added to description of the PR? I think it would be worthwhile to get a review from one of the designers as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jamigibbs I'm reaching out to Barb on this |
||
} | ||
&:hover { | ||
background-color: rgba(0, 0, 0, 0.05); | ||
} | ||
} | ||
} |
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.
Any idea why these props are showing up on the Storybook page like this? I don't think other pages do that.
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.
@jamigibbs - fixed!
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-harrison Awesome! Thanks for figuring that out! I think the only other thing that needs to be done is passing in an empty arguments object to the
Template
function so that the code examples populate:Maybe something like this here on this line and this line too? I can't exactly remember what has fixed this in the past.
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.
@jamigibbs - good catch, fixed!