Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion bestax-mcp/data/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@
"slug": "layout/level",
"import": "import { Level } from '@allxsmith/bestax-bulma';",
"compound": true,
"propCount": 26,
"propCount": 23,
"exampleCount": 5
},
{
Expand Down
29 changes: 1 addition & 28 deletions bestax-mcp/data/components/Level.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,37 +287,10 @@
"required": false,
"inherited": false,
"deprecated": false
},
{
"name": "href",
"type": "string",
"default": null,
"description": "Href for \"a\" tag.",
"required": false,
"inherited": false,
"deprecated": false
},
{
"name": "target",
"type": "string",
"default": null,
"description": "Target for \"a\" tag",
"required": false,
"inherited": false,
"deprecated": false
},
{
"name": "rel",
"type": "string",
"default": null,
"description": "Rel for \"a\" tag",
"required": false,
"inherited": false,
"deprecated": false
}
],
"extraProps": [],
"catchAll": "All standard <div> attributes and Bulma helper props",
"catchAll": "All standard <a> / <div> / <p> attributes and Bulma helper props",
"types": []
}
],
Expand Down
2 changes: 1 addition & 1 deletion bestax-mcp/data/components/SubTitle.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"deprecated": false
}
],
"catchAll": "All standard <h1>–<h6> attributes and Bulma helper props",
"catchAll": "All standard <h1>–<h6> / <p> attributes and Bulma helper props",
"types": []
}
],
Expand Down
2 changes: 1 addition & 1 deletion bestax-mcp/data/components/Title.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"deprecated": false
}
],
"catchAll": "All standard <h1>–<h6> attributes and Bulma helper props",
"catchAll": "All standard <h1>–<h6> / <p> attributes and Bulma helper props",
"types": []
}
],
Expand Down
6 changes: 3 additions & 3 deletions bestax-mcp/data/skills.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
{
"id": "bloomer-unmappables",
"file": "references/bloomer/unmappables.md",
"bytes": 16259
"bytes": 16354
},
{
"id": "css-migration",
Expand All @@ -118,7 +118,7 @@
{
"id": "rbx-unmappables",
"file": "references/rbx/unmappables.md",
"bytes": 13444
"bytes": 13539
},
{
"id": "react-bulma-components-component-map",
Expand All @@ -133,7 +133,7 @@
{
"id": "react-bulma-components-unmappables",
"file": "references/react-bulma-components/unmappables.md",
"bytes": 9448
"bytes": 9543
}
],
"examples": []
Expand Down
24 changes: 0 additions & 24 deletions bestax-migrate/e2e/as-unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,30 +381,6 @@ describe('the per-target link-attribute exception matches the library', () => {
diagnostics: '',
});
});

it('holds `Level.Item` to exactly the link attributes it declares', () => {
// Most href-capable targets either follow `as` or extend
// `AnchorHTMLAttributes`; this one enumerates, and the rule has a row for
// it. Asserted by use, both ways.
const rows: string[] = [
"import { Level } from '@allxsmith/bestax-bulma';",
'',
`export const ok = <Level.Item as="a" href="#" target="_blank">{'x'}</Level.Item>;`,
];
for (const attr of Object.keys(LINK_ATTR_TABLE)) {
if (TARGET_LINK_ATTR_TABLE['Level.Item'].includes(attr)) continue;
rows.push(`// @ts-expect-error Level.Item declares no ${attr}`);
rows.push(
`export const n_${attr} = <Level.Item as="a" ${attr}${SAMPLE[attr]}>{'x'}</Level.Item>;`
);
}
const source = rows.join('\n') + '\n';
const { status, diagnostics } = typecheckTsx(source, 'target-link-attrs');
expect({ status, diagnostics: annotate(diagnostics, source) }).toEqual({
status: 0,
diagnostics: '',
});
});
});

describe('the href-bearing intrinsics match what React types', () => {
Expand Down
29 changes: 22 additions & 7 deletions bestax-migrate/src/sources/_shared/polymorphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,35 @@ const LINK_ATTR_ELEMENTS: Record<string, readonly string[]> = {
/**
* The link attributes a target accepts, where its props do not follow `as`.
*
* Most components here either follow `as` (the element decides, above) or
* extend `AnchorHTMLAttributes` outright (`Pagination.*`, `Panel.Block`, so
* everything is fine). `Level.Item` is the exception that enumerates: it
* declares `href`, `target` and `rel` and nothing else, so `download`,
* `hrefLang`, `ping` and `referrerPolicy` are type errors there even at
* `as="a"`.
* Every component here either follows `as` (the element decides, above) or
* carries the anchor's attributes outright — `Pagination.*` and `Panel.Block`
* by extending `AnchorHTMLAttributes`, `Level.Item` since #672 by subtracting
* `HTMLAttributes` from it. So none is narrower than the element it renders,
* and none needs a row.
*
* A target that ENUMERATED a subset would be, and the rule would have to know:
* `Level.Item` listed `href`, `target` and `rel` and stopped, which made
* `download` a type error on its own `<a>`.
*
* A target absent from `HREF_OK` needs no row: a component that takes no
* `href` at any `as` takes none of its siblings either -- verified for
* `Card.FooterItem`, `Tabs.Item` and `Delete`, which is why this is derived
* from that table rather than being a second list to keep.
*/
const TARGET_LINK_ATTRS: Record<string, readonly string[]> = {
'Level.Item': ['target'],
// Empty since #672. `Level.Item` was the only row: it listed `href`, `target`
// and `rel` and stopped, so the element alone could not say that `download`
// was rejected. It now enumerates the anchor's attributes by SUBTRACTING
// `React.HTMLAttributes` from `React.AnchorHTMLAttributes`, which is all of
// them — and a row naming all of them decides nothing a row naming none does
// not, so the element decides and the row goes.
//
// The mechanism stays for a target that really is narrower than its element,
// and the e2e asserts the premise rather than assuming it: every href-capable
// target without a row accepts every link attribute on its anchor.
// `Level.Item` joins that sweep, so its coverage moved rather than going.
// NOTE that leaves the `rejectedBy = 'component'` arm with no live caller —
// worth deleting and restoring from git if nothing claims it.
};

/** The rows the type test holds to the library. */
Expand Down
55 changes: 55 additions & 0 deletions bulma-ui/src/__typetests__/polymorphic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
import React from 'react';
import { Avatar } from '../components/Avatar';
import { Level, type LevelItemProps } from '../layout/Level';
import {
Dropdown,
type DropdownItemElement,
Expand Down Expand Up @@ -341,3 +342,57 @@ export const rejected = (
<Link color="nope" />
</>
);

// --------------------------------------------------------------------------
// `Level.Item` (#672). It is NOT polymorphic: it enumerates the anchor's
// attributes by subtracting `HTMLAttributes` from `AnchorHTMLAttributes`, and
// withholds them at runtime for any tag that is not an `<a>`. Deriving them
// from `as` is the source-breaking half, deferred.
//
// These exist because the compatibility was checked with throwaway probes
// while the polymorphic version was written and reverted, and a probe proves
// nothing after it is deleted. Each line below is a shape that version broke.
// --------------------------------------------------------------------------

export const levelItemGained = (
<>
{/* #641's symptom, and the two a hand-written list kept missing. */}
<Level.Item as="a" href="/x" download="f" />
<Level.Item as="a" referrerPolicy="no-referrer" hrefLang="en" ping="/p" />
<Level.Item as="a" media="print" type="application/pdf" />
</>
);

// An event handler written against the ALIAS keeps its contextual type. The
// polymorphic version turned one handler over a union element into a union of
// three handler types, and this became an implicit `any`.
export const levelItemHandler: LevelItemProps = {
onClick: event => void event.currentTarget,
};

// A props bag spreads back into its own component — the canonical wrapper
// shape, and `TS2769` under the reverted version.
declare const levelItemBag: LevelItemProps;
export const levelItemSpread = <Level.Item {...levelItemBag} />;

// The alias stays assignable to a single element's attributes.
declare function wantsDivAttributes(
props: React.HTMLAttributes<HTMLDivElement>
): void;
export const levelItemAssignable = () => wantsDivAttributes(levelItemBag);

// `ComponentProps` keeps its keys reachable: `keyof` a union is the
// INTERSECTION of its members' keys, so a union-shaped props type loses them.
type LevelItemDerived = React.ComponentProps<typeof Level.Item>;
export const levelItemKey: keyof LevelItemDerived = 'href';
export const levelItemIndexed: LevelItemDerived['href'] = '/x';
export const levelItemDerivedTag: LevelItemDerived = { as: 'p' };

// And a memo wrapper keeps every tag.
const MemoLevelItem = React.memo(Level.Item);
export const levelItemMemo = (
<>
<MemoLevelItem as="a" href="/x" />
<MemoLevelItem as="p" />
</>
);
33 changes: 33 additions & 0 deletions bulma-ui/src/layout/Level.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,36 @@ export const CompoundUsage: StoryObj<typeof Level> = {
</Level>
),
};

/**
* `Level.Item` renders an `<a>` when told to, and that form takes the anchor's
* wider attributes — `download`, `hrefLang`, `ping`, `referrerPolicy` — not
* just `href`/`target`/`rel`. On any other `as` they are withheld rather than
* rendered as dead markup.
*/
export const AnchorItem: StoryObj<typeof Level> = {
render: () => (
<Level>
<Level.Left>
<Level.Item
as="a"
href="/files/report.pdf"
download="report.pdf"
referrerPolicy="no-referrer"
>
Download the report
</Level.Item>
</Level.Left>
<Level.Right>
<Level.Item
as="a"
href="https://example.com"
target="_blank"
rel="noreferrer"
>
Open example.com
</Level.Item>
</Level.Right>
</Level>
),
};
74 changes: 56 additions & 18 deletions bulma-ui/src/layout/Level.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,42 @@ export const LevelRight: React.FC<LevelRightProps> = ({
);
};

/**
* Exactly what `<a>` adds over the attributes every element has: `href`,
* `target`, `rel`, `download`, `hrefLang`, `ping`, `referrerPolicy`, `media`,
* `type`.
*
* Subtracted from React's own types rather than listed. Listing them is how this
* component came to reject `download` at `as="a"` — the list said `href`,
* `target`, `rel` and stopped, and #641 is the report. A list gains an entry only
* when someone notices; a subtraction gains it when React does.
*
* They reach the `<a>` branch and no other: declared at every `as` because
* narrowing them to the anchor is source-breaking (#672, `next-major`), and
* withheld at runtime so the type's permissiveness costs no dead markup.
*/
type AnchorOnlyAttributes = Omit<
React.AnchorHTMLAttributes<HTMLAnchorElement>,
keyof React.HTMLAttributes<HTMLAnchorElement>
>;
Comment thread
allxsmith marked this conversation as resolved.
Resolved

/**
* Props for the LevelItem component.
*
* The anchor attributes are enumerated here, at every `as`, and forwarded only
* when the tag is an `<a>` — so `<Level.Item as="p" download>` compiles and
* renders a `<p>` without it. That asymmetry is deliberate for now: deriving
* them from `as` instead is what makes the dead pair a type error, and it also
* stops `const p: LevelItemProps = { href }` compiling for consumers who write
* it today. #672 carries that half, labelled `next-major`.
*
* What this list must NOT become is a shorter one. Enumerating only `href`,
* `target` and `rel` made every other anchor attribute a type error even at
* `as="a"`, which is what #641 reported.
*/
export interface LevelItemProps
extends
AnchorOnlyAttributes,
Comment thread
allxsmith marked this conversation as resolved.
Resolved
React.HTMLAttributes<
HTMLDivElement | HTMLParagraphElement | HTMLAnchorElement
>,
Expand All @@ -185,12 +216,6 @@ export interface LevelItemProps
className?: string;
/** Content. */
children?: React.ReactNode;
/** Href for "a" tag. */
href?: string;
/** Target for "a" tag */
target?: string;
/** Rel for "a" tag */
rel?: string;
}

/**
Expand All @@ -205,9 +230,6 @@ export const LevelItem: React.FC<LevelItemProps> = ({
hasTextCentered,
className,
children,
href,
target,
rel,
color,
bgColor,
textColor,
Expand All @@ -225,23 +247,39 @@ export const LevelItem: React.FC<LevelItemProps> = ({
});
const levelItemClasses = classNames(mainClass, bulmaHelperClasses, className);

// If rendering as "a", only pass anchor-specific props
// The anchor's own attributes reach an `<a>` and nothing else. The type
// declares them at every `as` — narrowing that is source-breaking (#672) — so
// the runtime is what keeps `<Level.Item as="p" download>` from rendering a
// dead attribute. One list, because JS has no view of the type; #682 is the
// issue for sharing it with the two components that keep their own copies.
const ANCHOR_ONLY = [
'href',
'target',
'rel',
'download',
'hrefLang',
'ping',
'referrerPolicy',
'media',
'type',
] as const;

if (Tag === 'a') {
return (
<a
className={levelItemClasses}
href={href}
target={target}
rel={rel}
{...rest}
>
<a className={levelItemClasses} {...rest}>
{children}
</a>
);
}

const withoutAnchorAttrs = Object.fromEntries(
Object.entries(rest).filter(
([key]) => !(ANCHOR_ONLY as readonly string[]).includes(key)
)
);

return (
<Tag className={levelItemClasses} {...rest}>
<Tag className={levelItemClasses} {...withoutAnchorAttrs}>
{children}
</Tag>
);
Expand Down
Loading
Loading