Skip to content

Commit

Permalink
Support the Popover API
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-ka committed Jul 9, 2024
1 parent 390531d commit a06729b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,20 @@ An attribute starting with `on` will passed to [the event listeners module](http
// { on: { click: f } }
```

### `list` and `role`
### `list`, `role`, and `popoverTarget`

The `list` and the `role` attributes will be passed to [the attributes module](https://github.com/snabbdom/snabbdom#the-attributes-module).
The `list`, the `role`, and the `popoverTarget` attributes will be passed to [the attributes module](https://github.com/snabbdom/snabbdom#the-attributes-module).
Note that the attribute names will be lowercased.

```tsx
<div role="button" />
// { attrs: { role: 'button' } }

<input list="options" />
// { attrs: { list: 'options' } }

<button popoverTarget="popover" />
// { attrs: { popovertarget: 'popover' } }
```

### `$hook`
Expand Down
46 changes: 46 additions & 0 deletions src/jsx-runtime.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1032,4 +1032,50 @@ describe('automatic runtime', () => {
text: undefined,
});
});

test('popover API', () => {
expect(
<>
<button popoverTarget="popover" popoverTargetAction="toggle">
Toggle
</button>
<div popover>Hello, world!</div>
</>,
).toStrictEqual({
children: [
{
children: undefined,
data: {
attrs: {
popovertarget: 'popover',
},
props: {
popoverTargetAction: 'toggle',
},
},
elm: undefined,
key: undefined,
sel: 'button',
text: 'Toggle',
},
{
children: undefined,
data: {
props: {
popover: 'auto',
},
},
elm: undefined,
key: undefined,
sel: 'div',
text: 'Hello, world!',
},
],
data: {},
elm: undefined,
key: undefined,
sel: undefined,
text: undefined,
});
});
});
6 changes: 6 additions & 0 deletions src/jsx-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const canonicalizeVNodeData = (orig: VNodeData): VNodeData => {
} else if (key === 'list' || key === 'role') {
data.attrs ??= {};
data.attrs[key] = v;
} else if (key === 'popover' && v === true) {
data.props ??= {};
data.props['popover'] = 'auto';
} else if (key === 'popoverTarget') {
data.attrs ??= {};
data.attrs['popovertarget'] = v;
} else if (key === '$style' || key === 'style') {
data.style = v;
} else if (key.startsWith('$')) {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export declare namespace Internal {
type Dir = 'ltr' | 'rtl' | 'auto' | Whatever;
type EnterKeyHint = 'done' | 'enter' | 'go' | 'next' | 'previous' | 'search' | 'send' | Whatever;
type InputMode = 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal' | 'search' | Whatever;
type Popover = 'auto' | 'manual';

interface Props extends Element.Props {
accessKey?: string | undefined;
Expand All @@ -353,6 +354,7 @@ export declare namespace Internal {
noModule?: boolean | undefined;
nonce?: string | undefined;
outerText?: string | undefined;
popover?: Popover | true | undefined;
spellcheck?: boolean | undefined;
tabIndex?: number | string | undefined;
title?: string | undefined;
Expand Down Expand Up @@ -408,6 +410,7 @@ export declare namespace Internal {
}

namespace HTMLButtonElement {
type PopoverTargetAction = 'hide' | 'show' | 'toggle';
type Type = 'button' | 'reset' | 'submit' | Whatever;

interface Props extends HTMLElement.Props {
Expand All @@ -419,6 +422,8 @@ export declare namespace Internal {
formNoValidate?: boolean | undefined;
formTarget?: string | undefined;
name?: string | undefined;
popoverTarget?: string | undefined;
popoverTargetAction?: PopoverTargetAction | undefined;
type?: Type | undefined;
value?: string;
}
Expand Down

0 comments on commit a06729b

Please sign in to comment.