Skip to content
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

docs: add examples for loading indicator inside button #3213

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/elements/button/button/button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { Args, ArgTypes, Meta, StoryContext, StoryObj } from '@storybook/we
import {
buttonDefaultArgs,
buttonDefaultArgTypes,
loading,
loading2,
requestSubmit,
} from '../common/button-common-stories.js';
import {
Expand Down Expand Up @@ -47,6 +49,8 @@ export const FixedWidth: StoryObj = fixedWidth;
export const WithSlottedIcon: StoryObj = withSlottedIcon;
export const LoadingIndicator: StoryObj = loadingIndicator;
export const RequestSubmit: StoryObj = requestSubmit;
export const Loading: StoryObj = loading;
export const Loading2: StoryObj = loading2;
export const WithHiddenSlottedIcon: StoryObj = withHiddenSlottedIcon;

const meta: Meta = {
Expand Down
80 changes: 80 additions & 0 deletions src/elements/button/common/button-common-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { commonDefaultArgs, commonDefaultArgTypes } from './common-stories.js';

import '../../action-group.js';
import '../../form-field.js';
import '../../loading-indicator-circle.js';

/* eslint-disable lit/binding-positions, @typescript-eslint/naming-convention */
const FormTemplate = ({
Expand Down Expand Up @@ -44,6 +45,77 @@ const FormTemplate = ({
<div id="form-data"></div>
</form>`;

/* eslint-disable lit/binding-positions, @typescript-eslint/naming-convention */
const LoadingTemplate = ({
tag,
type: _type,
reset: _reset,
'icon-name': _iconName,
...args
}: Args): TemplateResult => html`
<${unsafeStatic(tag)} ${sbbSpread(args)} aria-busy="false"
@click=${(e: PointerEvent) => {
const button = (e.target as HTMLElement)!;
const loadingIndicator = button.parentElement!.querySelector(
'sbb-loading-indicator-circle',
)!;

if (button.getAttribute('aria-busy') === 'false') {
button.setAttribute('aria-busy', 'true');
button.setAttribute('aria-disabled', 'true');
button.style.setProperty(
'--sbb-button-color-default-text',
'transparent',
);
button.style.setProperty('pointer-events', 'none');
loadingIndicator.style.removeProperty('display');

setTimeout(() => {
button.setAttribute('aria-busy', 'false');
button.setAttribute('aria-disabled', 'false');
button.style.removeProperty('--sbb-button-color-default-text');
button.style.removeProperty('pointer-events');
loadingIndicator.style.setProperty('display', 'none');
}, 5000);
}
}}>
Click to submit
<sbb-loading-indicator-circle aria-hidden style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display:none"></sbb-loading-indicator-circle>
</${unsafeStatic(tag)}>
`;

/* eslint-disable lit/binding-positions, @typescript-eslint/naming-convention */
const LoadingTemplate2 = ({
tag,
type: _type,
reset: _reset,
'icon-name': _iconName,
...args
}: Args): TemplateResult => html`
<${unsafeStatic(tag)} ${sbbSpread(args)} aria-busy="false"
@click=${(e: PointerEvent) => {
const button = (e.target as HTMLElement)!;
const loadingIndicator = button.parentElement!.querySelector(
'sbb-loading-indicator-circle',
)!;

if (button.getAttribute('aria-busy') === 'false') {
button.setAttribute('aria-busy', 'true');
button.setAttribute('aria-disabled', 'true');
loadingIndicator.style.setProperty('width', '20px');

setTimeout(() => {
button.setAttribute('aria-busy', 'false');
button.setAttribute('aria-disabled', 'false');
loadingIndicator.style.setProperty('width', '0px');
}, 5000);
}
}}>
<sbb-loading-indicator-circle aria-hidden style="width:0; overflow-x: clip; transition: width var(--sbb-animation-duration-2x) var(--sbb-animation-easing)"></sbb-loading-indicator-circle>
Click to submit
</${unsafeStatic(tag)}>
`;

/* eslint-enable lit/binding-positions, @typescript-eslint/naming-convention */

const type: InputType = {
Expand Down Expand Up @@ -133,3 +205,11 @@ export const requestSubmit: StoryObj = {
render: FormTemplate,
args: { text: undefined, type: undefined, value: 'submit button' },
};

export const loading: StoryObj = {
render: LoadingTemplate,
};

export const loading2: StoryObj = {
render: LoadingTemplate2,
};
Loading