(attr) - JavaScript
JSX attributes on opening tags
clojure
(attr key? val?)Where:
- key: attribute name (the
classNameinclassName="active"orsvg:hrefinsvg:href="...") * - val: attribute value or expression *
Matching:
- string literal values, e.g.
className="active" - expression values, e.g.
onClick={handler} - boolean attributes, e.g.
disabled
Behavior
- If a value selector is provided, boolean attributes without a value do not match.
- Attribute values inside
{...}are matched against the inner expression.
Examples
Find external links that open in a new tab.
clojure
(jsx a (attr target (str "_blank")))Selects in lines { 1 } but not in { 2, 3 }:
typescript
<a target="_blank" href="...">Docs</a>
<a target="_self" href="...">Home</a>
<Link target="_blank" />Locate buttons that still use inline click handlers.
clojure
(jsx Button (attr onClick (id handleClick)))Selects in lines { 1 } but not in { 2, 3 }:
typescript
<Button onClick={handleClick}>Save</Button>
<Button onClick={track}>Save</Button>
<Link onClick={handleClick}>Save</Link>Find SVG elements using xlink:href before a migration.
clojure
(jsx _ (attr xlink:href))Selects in lines { 1 } but not in { 2, 3 }:
typescript
<use xlink:href="#icon-user" />
<use href="#icon-user" />
<img xlink:href="#icon-user" />Arguments
key
- Identifier: shorthand for (id)
- Pattern: shorthand for (id)
- (id)
- (:into)
- (:nearest)
- (:and)
- (:or)
- (:not)
- (:text)
- (:kind)
- (:replace)
- (:replace-in)
- (:capture)