-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(atomic): add heading function for lit components (#4861)
* Added `heading` function to replace the `<Heading>` functional component. * Renamed `hidden.tsx` function component to `stencil-hidden.tsx` (to be consistent) ## Usage ### with Stencil ```.tsx <Heading level={level} class="truncate"> {label} </Heading> ``` ### with Lit ```ts html`${heading( {level, class: "truncate"}, label )}` ``` ## Notes Using [`unsafeStatic`](https://lit.dev/docs/templates/expressions/#non-literal-statics) since we need to interpolate non-literal statics. https://coveord.atlassian.net/browse/KIT-3833 --------- Co-authored-by: Frederic Beaudoin <[email protected]>
- Loading branch information
1 parent
9c846ba
commit 33fae2f
Showing
35 changed files
with
141 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {within} from '@storybook/test'; | ||
import {html, render} from 'lit'; | ||
import {heading, HeadingProps} from './heading'; | ||
|
||
describe('heading', () => { | ||
let container: HTMLElement; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
const renderHeading = ( | ||
props: Partial<HeadingProps>, | ||
children?: string | ||
): HTMLElement => { | ||
render( | ||
html`${heading( | ||
{ | ||
...props, | ||
level: props.level ?? 1, | ||
}, | ||
html`${children}` | ||
)}`, | ||
container | ||
); | ||
return within(container).getByRole( | ||
props.level && props.level > 0 && props.level <= 6 ? 'heading' : 'generic' | ||
) as HTMLElement; | ||
}; | ||
|
||
it('should render a heading in the document', () => { | ||
const props = {level: 1}; | ||
const headingElement = renderHeading(props); | ||
expect(headingElement).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render a heading with the correct level', () => { | ||
const props = {level: 2}; | ||
const headingElement = renderHeading(props); | ||
expect(headingElement.tagName.toLowerCase()).toBe('h2'); | ||
}); | ||
|
||
it('should render a div if level is outside the range of 1 to 6', () => { | ||
const props = {level: 0}; | ||
const headingElement = renderHeading(props); | ||
expect(headingElement.tagName.toLowerCase()).toBe('div'); | ||
}); | ||
|
||
it('should render a heading with the correct text content', () => { | ||
const props = {level: 1}; | ||
const headingElement = renderHeading(props, 'Test Heading'); | ||
expect(headingElement.textContent).toContain('Test Heading'); | ||
}); | ||
|
||
it('should apply additional classes', () => { | ||
const props = {level: 1, class: 'test-class'}; | ||
const headingElement = renderHeading(props); | ||
expect(headingElement).toHaveClass('test-class'); | ||
}); | ||
|
||
it('should apply part attribute', () => { | ||
const props = {level: 1, part: 'heading-part'}; | ||
const headingElement = renderHeading(props); | ||
expect(headingElement.getAttribute('part')).toBe('heading-part'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {ifDefined} from 'lit-html/directives/if-defined.js'; | ||
import {html, literal, unsafeStatic} from 'lit/static-html.js'; | ||
|
||
export interface HeadingProps { | ||
/** | ||
* The heading level. | ||
* | ||
* A value outside of the range of 1 to 6 will render a div instead of a heading. | ||
*/ | ||
level: number; | ||
/** | ||
* Additional classes to add to the heading. | ||
*/ | ||
class?: string; | ||
/** | ||
* Additional parts to add to the heading. | ||
*/ | ||
part?: string; | ||
} | ||
|
||
export const heading = <T>( | ||
{level, class: classname, part}: HeadingProps, | ||
children?: T | ||
) => { | ||
const headingTag = | ||
level > 0 && level <= 6 ? unsafeStatic(`h${level}`) : literal`div`; | ||
|
||
return html`<${headingTag} class="${ifDefined(classname)}" part="${ifDefined(part)}"> | ||
${children} | ||
</${headingTag}>`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...atomic/src/components/common/smart-snippets/atomic-smart-snippet/smart-snippet-common.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 5 additions & 10 deletions
15
.../atomic/src/components/common/heading.tsx → ...src/components/common/stencil-heading.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/atomic/src/components/common/tab-manager/tab-guard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.