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

App-button component #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
export namespace Components {
interface AppButton {
"label": string;
"variant": 'primary' | 'secondary';
}
interface AppHome {
}
interface AppProfile {
Expand All @@ -15,6 +19,12 @@ export namespace Components {
}
}
declare global {
interface HTMLAppButtonElement extends Components.AppButton, HTMLStencilElement {
}
var HTMLAppButtonElement: {
prototype: HTMLAppButtonElement;
new (): HTMLAppButtonElement;
};
interface HTMLAppHomeElement extends Components.AppHome, HTMLStencilElement {
}
var HTMLAppHomeElement: {
Expand All @@ -34,12 +44,17 @@ declare global {
new (): HTMLAppRootElement;
};
interface HTMLElementTagNameMap {
"app-button": HTMLAppButtonElement;
"app-home": HTMLAppHomeElement;
"app-profile": HTMLAppProfileElement;
"app-root": HTMLAppRootElement;
}
}
declare namespace LocalJSX {
interface AppButton {
"label"?: string;
"variant"?: 'primary' | 'secondary';
}
interface AppHome {
}
interface AppProfile {
Expand All @@ -48,6 +63,7 @@ declare namespace LocalJSX {
interface AppRoot {
}
interface IntrinsicElements {
"app-button": AppButton;
"app-home": AppHome;
"app-profile": AppProfile;
"app-root": AppRoot;
Expand All @@ -57,6 +73,7 @@ export { LocalJSX as JSX };
declare module "@stencil/core" {
export namespace JSX {
interface IntrinsicElements {
"app-button": LocalJSX.AppButton & JSXBase.HTMLAttributes<HTMLAppButtonElement>;
"app-home": LocalJSX.AppHome & JSXBase.HTMLAttributes<HTMLAppHomeElement>;
"app-profile": LocalJSX.AppProfile & JSXBase.HTMLAttributes<HTMLAppProfileElement>;
"app-root": LocalJSX.AppRoot & JSXBase.HTMLAttributes<HTMLAppRootElement>;
Expand Down
23 changes: 23 additions & 0 deletions src/components/app-button/app-button.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
:host {
display: block;
}

::slotted([slot='label'])
{
display: flex;
gap: 8px;
}

.btn-primary {
background-color: red;
color: white;
}
.btn-secondary {
background-color: black;
color: white;
}

.button-content {
display: flex;
gap: 8px;
}
25 changes: 25 additions & 0 deletions src/components/app-button/app-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Component, Host, h, Prop } from '@stencil/core';

@Component({
tag: 'app-button',
styleUrl: 'app-button.css',
shadow: true,
})
export class AppButton {
@Prop() label: string
@Prop() variant: 'primary' | 'secondary'
render() {
return (
<Host>
<button class={`btn-${this.variant}`}>
<div class="button-content">
{this.variant}
<div>
<slot name="label">{this.label}</slot>
</div>
</div>
</button>
</Host>
);
}
}
26 changes: 26 additions & 0 deletions src/components/app-button/test/app-button.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { newE2EPage } from '@stencil/core/testing';

describe('app-button component', () => {
it('should render successfully', async () => {
const page = await newE2EPage();
await page.setContent('<app-button></app-button>');

const element = await page.find('app-button');
expect(element).toHaveClass('hydrated');
});

it('should apply primary variant class', async () => {
const page = await newE2EPage();
await page.setContent('<app-button variant="primary"></app-button>');

const element = await page.find('app-button');
// / Ensures component is rendered
expect(element).toHaveClass('hydrated');

// Find the shadow DOM button element
const button = await page.find('app-button >>> button');

// Check if the button inside shadow DOM has the class `btn-primary`
expect(button).toHaveClass('btn-primary');
});
});
20 changes: 20 additions & 0 deletions src/components/app-button/test/app-button.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { newSpecPage } from '@stencil/core/testing';
import { AppButton } from '../app-button';

describe('app-button', () => {
it('renders', async () => {
const page = await newSpecPage({
components: [AppButton],
html: `<app-button>
<slot></slot>
</app-button>`,
});
expect(page.root).toEqualHtml(`
<app-button>
<mock:shadow-root>
<slot></slot>
</mock:shadow-root>
</app-button>
`);
});
});
4 changes: 4 additions & 0 deletions src/components/app-home/app-home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class AppHome {
>
Profile Page
</button>

<app-button label='Machado' variant='primary'>
Rudis
</app-button>
</div>
);
}
Expand Down