|
| 1 | +# no-invalid-bodytext-parent |
| 2 | + |
| 3 | +Disallow `BodyText` with a block-level tag inside elements that cannot contain block-level content. |
| 4 | + |
| 5 | +## Rule Details |
| 6 | + |
| 7 | +`BodyText` renders as a `<p>` element by default. HTML does not allow block-level |
| 8 | +elements like `<p>` inside inline contexts such as `<button>`, `<label>`, `<p>`, |
| 9 | +or heading elements. Placing `BodyText` inside these elements produces invalid |
| 10 | +markup that browsers must silently repair, which can cause layout or accessibility |
| 11 | +issues. |
| 12 | + |
| 13 | +Add `tag="span"` (or another inline tag) to `BodyText` to make it render as an |
| 14 | +inline element that is valid in these contexts. |
| 15 | + |
| 16 | +This rule provides an auto-fix that adds `tag="span"` when the fix is |
| 17 | +unambiguous. Dynamic `tag` expressions (e.g. `tag={isLabel ? "span" : "p"}`) are |
| 18 | +not auto-fixed to avoid silently corrupting conditional logic. |
| 19 | + |
| 20 | +Examples of **incorrect** code: |
| 21 | + |
| 22 | +```tsx |
| 23 | +/* BodyText inside a <button> */ |
| 24 | +<button> |
| 25 | + <BodyText>Click me</BodyText> |
| 26 | +</button> |
| 27 | + |
| 28 | +/* BodyText inside a WB Button component */ |
| 29 | +<Button onClick={handleClick}> |
| 30 | + <BodyText>Click me</BodyText> |
| 31 | +</Button> |
| 32 | + |
| 33 | +/* BodyText inside a <label> */ |
| 34 | +<label> |
| 35 | + <BodyText>Email address</BodyText> |
| 36 | + <input type="email" /> |
| 37 | +</label> |
| 38 | + |
| 39 | +/* BodyText inside a WB form component */ |
| 40 | +<Choice label={<BodyText>Option A</BodyText>} value="a" /> |
| 41 | +<LabeledField label={<BodyText>Email</BodyText>} field={<input />} /> |
| 42 | +<RadioGroup description={<BodyText>Choose one</BodyText>} /> |
| 43 | +<CheckboxGroup description={<BodyText>Select all that apply</BodyText>} /> |
| 44 | + |
| 45 | +/* BodyText nested inside another BodyText (both render as <p>) */ |
| 46 | +<BodyText> |
| 47 | + Outer text <BodyText>inner text</BodyText> |
| 48 | +</BodyText> |
| 49 | + |
| 50 | +/* BodyText inside a heading */ |
| 51 | +<h2> |
| 52 | + Section <BodyText>subtitle</BodyText> |
| 53 | +</h2> |
| 54 | +<StyledH2> |
| 55 | + Section <BodyText>subtitle</BodyText> |
| 56 | +</StyledH2> |
| 57 | +<Heading> |
| 58 | + Section <BodyText>subtitle</BodyText> |
| 59 | +</Heading> |
| 60 | +``` |
| 61 | + |
| 62 | +Examples of **correct** code: |
| 63 | + |
| 64 | +```tsx |
| 65 | +/* Inline tag makes BodyText valid inside a button */ |
| 66 | +<button> |
| 67 | + <BodyText tag="span">Click me</BodyText> |
| 68 | +</button> |
| 69 | + |
| 70 | +/* Inline tag makes BodyText valid inside a WB Button */ |
| 71 | +<Button onClick={handleClick}> |
| 72 | + <BodyText tag="span">Click me</BodyText> |
| 73 | +</Button> |
| 74 | + |
| 75 | +/* Inline tag makes BodyText valid inside a label */ |
| 76 | +<label> |
| 77 | + <BodyText tag="span">Email address</BodyText> |
| 78 | + <input type="email" /> |
| 79 | +</label> |
| 80 | + |
| 81 | +/* Inline tag makes BodyText valid inside a WB form component */ |
| 82 | +<Choice label={<BodyText tag="span">Option A</BodyText>} value="a" /> |
| 83 | + |
| 84 | +/* Outer BodyText uses a block-container tag, allowing block children */ |
| 85 | +<BodyText tag="div"> |
| 86 | + Outer text <BodyText>inner text</BodyText> |
| 87 | +</BodyText> |
| 88 | + |
| 89 | +/* Inline tag makes BodyText valid inside a heading */ |
| 90 | +<h2> |
| 91 | + Section <BodyText tag="sup">note</BodyText> |
| 92 | +</h2> |
| 93 | + |
| 94 | +/* BodyText is fine at the top level or inside block containers */ |
| 95 | +<BodyText>Standalone paragraph</BodyText> |
| 96 | +<div> |
| 97 | + <BodyText>Inside a div</BodyText> |
| 98 | +</div> |
| 99 | +``` |
| 100 | + |
| 101 | +## When Not To Use It |
| 102 | + |
| 103 | +Disable this rule only if you are intentionally rendering invalid HTML and have a |
| 104 | +specific reason to do so. In most cases the auto-fix resolves the issue without |
| 105 | +any manual effort. |
0 commit comments