Skip to content

Commit

Permalink
back to Fragment
Browse files Browse the repository at this point in the history
what i think happened was:

previously, TS didnt strongly type the `jsxFragmentFactory` function
meaning the minimal jsx runtime could abuse things by passing in the
`DocumentFragment` constructor as a sentinel to know when to call
`document.createDocumentFragment();`. even though `DocumentFragment`
didn't properly match an `ElementFunction`'s signature, we didn't
really care bc we just used it as a sentinel. now TS *does* complain
about this, so it really wants the function to actually be an
element function.

it seems like making `Fragment` just ignore props and do
`createDocumentFragment` kind of just works? only props that should
matter is `props.children`, only im pretty sure `children` still
gets passed to `h` anyway afaict?
  • Loading branch information
scheibo committed Dec 16, 2024
1 parent a61b063 commit 07cd452
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/tools/display/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as engine from '../../pkg';
import {LAYOUT, LE} from '../../pkg/data';
import * as gen1 from '../../pkg/gen1';

import {Fragment} from './dom';
import {Battle, Gen, Generation, adapt} from './ui';
import * as util from './util';

Expand Down Expand Up @@ -99,11 +100,11 @@ const App = ({gen, data, error, seed}: {
}
frames.push(<Frame frame={partial || {}} gen={gen} showdown={showdown} last={last} />);

return <div>
return <>
{!!seed && <h1>0x{seed.toString(16).toUpperCase()}</h1>}
{frames}
{error && <pre className='error'><code>{error}</code></pre>}
</div>;
</>;
};

const Frame = ({frame, gen, showdown, last}: {
Expand Down
13 changes: 5 additions & 8 deletions src/tools/display/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @license MIT modified from Vadim Demedes's https://github.com/vadimdemedes/dom-chef */
type Attributes = JSX.IntrinsicElements['div'];
type DocumentFragmentConstructor = typeof DocumentFragment;
type DocumentFragmentFunction = (props?: any) => DocumentFragment;
type ElementFunction = (props?: any) => HTMLElement;

declare global {
Expand All @@ -22,9 +22,6 @@ interface Fragment {
// https://github.com/preactjs/preact/blob/1bbd687c/src/constants.js#L3
const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;

const isFragment = (type: DocumentFragmentConstructor | ElementFunction):
type is DocumentFragmentConstructor => type === DocumentFragment;

const setCSSProps = (element: HTMLElement, style: CSSStyleDeclaration) => {
for (const [name, value] of Object.entries(style)) {
if (name.startsWith('-')) {
Expand Down Expand Up @@ -57,12 +54,12 @@ const addChildren = (parent: Element | DocumentFragment, children: Node[]) => {
const FALSIFIABLE_ATTRIBUTES = ['contentEditable', 'draggable', 'spellCheck', 'value'];

export const h = (
type: DocumentFragmentConstructor | ElementFunction | string,
type: DocumentFragmentFunction | ElementFunction | string,
attributes?: Attributes,
...children: Node[]
) => {
if (typeof type !== 'string') {
const element = isFragment(type) ? document.createDocumentFragment() : type(attributes);
const element = type(attributes);
addChildren(element, children);
return element;
}
Expand Down Expand Up @@ -92,5 +89,5 @@ export const h = (
return element;
};

export const Fragment =
(typeof DocumentFragment === 'function' ? DocumentFragment : () => {}) as Fragment;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const Fragment = (unused: any) => document.createDocumentFragment();
5 changes: 3 additions & 2 deletions src/tools/display/select.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @license MIT modified from Pixabay's https://github.com/Pixabay/JavaScript-autoComplete */
import {Fragment} from './dom';

const FRAME_MS = 20;
// const DELAY_MS = 150;
Expand All @@ -19,8 +20,8 @@ export const Select = ({options, unmount, placeholder, render}: {

render ||= option => input.value
// ? <span dangerouslySetInnerHTML={{__html: searcher.highlight(option)}}></span>
? <span>{option}</span>
: <span>{option}</span>;
? <>{option}</>
: <>{option}</>;

let maxHeight = 0;
let offsetHeight = 0;
Expand Down

0 comments on commit 07cd452

Please sign in to comment.