Skip to content

Commit 5a69c92

Browse files
committed
feat: button web component
1 parent 7c2d296 commit 5a69c92

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

examples/react/basic/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function App() {
3232
<div>
3333
<h1>TanStack Devtools React Basic Example</h1>
3434
<tsd-json-tree value={JSON.stringify(value)} />
35+
<tsd-button text="test" ghost={true} outline={true} value='test' variant="secondary" onClick={() => console.log('Button clicked!')} />
3536
<Devtools />
3637
</div>
3738
)

examples/solid/basic/src/setup.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const rootRoute = createRootRoute({
2525
<tsd-json-tree
2626
value={{ title: 'Test Event', description: 'This is a test event.' }}
2727
/>
28+
<tsd-button text="test" value='test' variant="secondary" onClick={() => console.log('Button clicked!')} />
2829
<hr />
2930
<Outlet />
3031
</>

packages/devtools-ui/src/components/button.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { splitProps } from 'solid-js'
1+
import { Show, createEffect, createSignal, splitProps } from 'solid-js'
22
import clsx from 'clsx'
3+
import { customElement, noShadowDOM } from 'solid-element'
34
import { useStyles } from '../styles/use-styles'
45
import type { JSX } from 'solid-js'
56

@@ -10,12 +11,14 @@ export type ButtonVariant =
1011
| 'success'
1112
| 'info'
1213
| 'warning'
13-
type ButtonProps = JSX.ButtonHTMLAttributes<HTMLButtonElement> & {
14+
15+
export type ButtonProps = JSX.ButtonHTMLAttributes<HTMLButtonElement> & {
1416
variant?: ButtonVariant
1517
outline?: boolean
1618
ghost?: boolean
1719
children?: any
1820
className?: string
21+
text?: string
1922
}
2023

2124
export function Button(props: ButtonProps) {
@@ -40,3 +43,27 @@ export function Button(props: ButtonProps) {
4043
</button>
4144
)
4245
}
46+
47+
export interface ButtonWebComponentProps extends Exclude<ButtonProps, "children"> {
48+
text: string
49+
}
50+
51+
export const registerButtonComponent = (elName: string = 'tsd-button') =>
52+
customElement<ButtonWebComponentProps>(elName, { variant: 'primary', outline: false, ghost: false, text: "" }, (props, { element }) => {
53+
noShadowDOM()
54+
const [buttonProps, setButtonProps] = createSignal(props)
55+
56+
createEffect(() => {
57+
element.addPropertyChangedCallback((name, value) => {
58+
setButtonProps((prev) => ({ ...prev, [name]: value }))
59+
})
60+
})
61+
62+
return (
63+
<Show keyed when={buttonProps()}>
64+
<Button {...buttonProps()}>
65+
{buttonProps().text}
66+
</Button>
67+
</Show>
68+
)
69+
})

packages/devtools-ui/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export { Input } from './components/input'
33
export { Select } from './components/select'
44
export { TanStackLogo } from './components/logo'
55
export { JsonTree, registerJsonTreeComponent } from './components/tree'
6-
export { Button } from './components/button'
6+
export { Button, registerButtonComponent } from './components/button'
77
export { Tag } from './components/tag'
88
export type { JsonTreeWebComponentProps } from './components/tree'
9+
export type { ButtonProps } from './components/button'
910
export type { CustomElements } from './types'

packages/devtools-ui/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { ButtonWebComponentProps } from './components/button'
12
import type { JsonTreeWebComponentProps } from './components/tree'
23

34
export interface CustomElements {
45
'tsd-json-tree': JsonTreeWebComponentProps
6+
'tsd-button': ButtonWebComponentProps
57
}

packages/devtools/src/core.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { lazy } from 'solid-js'
22
import { Portal, render } from 'solid-js/web'
33
import { ClientEventBus } from '@tanstack/devtools-event-bus/client'
4-
import { registerJsonTreeComponent } from '@tanstack/devtools-ui'
4+
import { registerButtonComponent, registerJsonTreeComponent } from '@tanstack/devtools-ui'
55
import { DevtoolsProvider } from './context/devtools-context'
66
import { initialState } from './context/devtools-store'
77
import type { ClientEventBusConfig } from '@tanstack/devtools-event-bus/client'
@@ -71,6 +71,7 @@ export class TanStackDevtoolsCore {
7171
this.#eventBus = new ClientEventBus(this.#eventBusConfig)
7272
this.#eventBus.start()
7373
registerJsonTreeComponent()
74+
registerButtonComponent()
7475
return (
7576
<DevtoolsProvider plugins={this.#plugins} config={this.#config}>
7677
<Portal mount={mountTo}>

0 commit comments

Comments
 (0)