-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
157 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { disclosure } from '../utils/disclosure'; | ||
|
||
export default class WebDisclosure extends HTMLElement { | ||
private trigger: HTMLButtonElement | null; | ||
private content: HTMLElement | null; | ||
private bindEscapeKey?: boolean; | ||
private bindClickOutside?: boolean; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.trigger = this.querySelector('[trigger]'); | ||
this.content = this.querySelector('[content]'); | ||
this.bindEscapeKey = this.hasAttribute('bind-escape-key'); | ||
this.bindClickOutside = this.hasAttribute('bind-click-outside'); | ||
|
||
this.init(); | ||
|
||
// NOTE: There are NO event listeners here. All events are handled by the external 'discloure()' dependency. | ||
} | ||
|
||
private init(): void { | ||
if (!this.trigger || !this.content) return; | ||
|
||
const button = this.trigger; | ||
const content = this.content; | ||
const bindEscapeKey = this.bindEscapeKey; | ||
const bindClickOutside = this.bindClickOutside; | ||
|
||
disclosure({ | ||
button, | ||
content, | ||
bindEscapeKey, | ||
bindClickOutside, | ||
}); | ||
} | ||
} |
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 @@ | ||
@use 'web-disclosure'; |
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,11 @@ | ||
/* | ||
---------------------------------------------------------------------------- | ||
Dependencies. | ||
---------------------------------------------------------------------------- | ||
*/ | ||
// @use '../base' as *; | ||
// @use '../mixins' as *; | ||
|
||
web-disclosure { | ||
// | ||
} |
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
13 changes: 13 additions & 0 deletions
13
ui/stories/6. Web Components Or Custom Elements/WebComponents.mdx
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,13 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
<Meta title="Web Components Or Custom Elements/Using Web Components" /> | ||
|
||
# HTML Web Components | ||
- The Web Components in this boilerplate are simply `custom elements` that wrap normal HTML markup, therefore providing a progressive enhancement layer via JavaScript. | ||
- They are **not** empty shells that only work with JavaScript. | ||
- They also don't make use of the [shadow DOM](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM). | ||
|
||
## Additional reading | ||
- Great explanation by Jeremy Keith (and others) about the power of [HTML Web Components](https://adactio.com/journal/20618). | ||
- Simple example from Chris Ferdinandi showing how to [create a Web Component from scratch](https://gomakethings.com/lets-create-a-web-component-from-scratch/). He also has lots more articles on this topic in his blog. | ||
- Some notes on the recommended use of [connectedCallback() lifecycle method and event listeners](https://hawkticehurst.com/writing/you-are-probably-using-connectedcallback-wrong/). |
21 changes: 21 additions & 0 deletions
21
ui/stories/6. Web Components Or Custom Elements/WebDisclosure/WebDisclosure.js
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,21 @@ | ||
export const WebDisclosureHtml = (args) => ` | ||
<web-disclosure | ||
class="disclosure" | ||
${args.bindEscapeKey === true ? 'bind-escape-key' : ''} | ||
${args.bindClickOutside === true ? 'bind-click-outside' : ''} | ||
> | ||
<button | ||
type="button" | ||
class="button button--text" | ||
trigger | ||
hidden | ||
> | ||
<span>Show / Hide</span> | ||
</button> | ||
<div content> | ||
<p>Content to be shown/hidden.</p> | ||
<p>Use this component when <code>accordion</code> or <code>tabs</code> components cannot be used.</p> | ||
</div> | ||
</web-disclosure> | ||
`; |
12 changes: 12 additions & 0 deletions
12
ui/stories/6. Web Components Or Custom Elements/WebDisclosure/WebDisclosure.mdx
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,12 @@ | ||
import { Meta, Canvas, Controls } from '@storybook/blocks'; | ||
import * as WebDisclosure from './WebDisclosure.stories'; | ||
|
||
<Meta of={WebDisclosure} /> | ||
|
||
# `<web-disclosure>` | ||
- This is functionally equivalent to the [disclosure](/story/components-disclosure--disclosure) component, with the same accessibility considerations. | ||
- The attributes that bind clicking the `ESC` key and clicking outside are renamed to `bind-escape-key` and `bind-click-outside`. | ||
- To maintain the [DRY code](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) approach in this boilerplate, the JavaScript logic to handle showing/hiding of content is imported from an external shared utility function in `javascript/utils/disclosure.ts`. | ||
|
||
<Canvas of={WebDisclosure.WebDisclosure} /> | ||
<Controls of={WebDisclosure.WebDisclosure} /> |
25 changes: 25 additions & 0 deletions
25
ui/stories/6. Web Components Or Custom Elements/WebDisclosure/WebDisclosure.stories.js
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,25 @@ | ||
import { WebDisclosureHtml } from './WebDisclosure'; | ||
|
||
export default { | ||
title: 'Web Components Or Custom Elements/<web-disclosure>', | ||
parameters: { | ||
status: { | ||
type: 'stable', | ||
}, | ||
}, | ||
argTypes: { | ||
bindEscapeKey: { | ||
control: 'boolean', | ||
description: 'Close with ESC key.' | ||
}, | ||
bindClickOutside: { | ||
control: 'boolean', | ||
description: 'Close by clicking outside' | ||
}, | ||
}, | ||
}; | ||
|
||
export const WebDisclosure = { | ||
render: (args) => WebDisclosureHtml(args), | ||
}; | ||
WebDisclosure.storyName = '<web-disclosure>'; |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
ui/stories/6. Utilities/Utilities.stories.js → ui/stories/7. Utilities/Utilities.stories.js
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
File renamed without changes.
File renamed without changes.