Utilities
Element
BitElement renders whatever HTML tag the Element parameter names - a div by default, but just as easily an anchor, a button, an input, an SVG shape or a custom element - while still accepting the styling, direction and state parameters every other component of the library offers. Everything that is not a parameter is splatted onto that tag, so attributes and event handlers land on whichever element is rendered, and the class and style it builds are merged with the ones written as plain HTML attributes. The tag then decides the rest: a void element holds no content, a disabled element is disabled the way its own tag allows, and NoWrapper drops the tag altogether and leaves only the content behind. On top of that it reaches what Razor offers on a plain element and not on a component: the stopPropagation and preventDefault modifiers of the click and of any other event.
Usage
Every example is live. Open its code to see exactly what produced the component running underneath.
Basic
div: it carries the bit-elm class, an
automatically generated id and whatever content is put inside it. That is the whole of the
component with nothing configured, and every other example only changes which tag that content ends up in.
Element
linearGradient are case sensitive. What is accepted is a name a tag can be made of: an ASCII
letter, followed by letters, digits and the -, _, . and :
that join them in every markup language that has tag names. Anything else falls back to the default
div rather than reaching the markup - a whitespace or a < would end the tag and
write markup of its own, and the remaining symbols are names the browsers do not agree to build an element
of, so one of them would throw where the element is created. Changing the tag between renders replaces the
rendered element with a new one.
A heading (h4)
A paragraph (p) with a highlighted (mark) word in it.
A quotation (blockquote)
A code span (code)
SVG & custom elements
svg the element is rendered into, and the case of the tag name is kept, which is what
linearGradient, clipPath and feGaussianBlur need. A custom element -
any name with a dash in it - is passed through untouched, so a web component is reachable from Razor with the
same parameters as everything else.
Void elements
area, base, br,
col, embed, hr, img, input, link,
meta, param, source, track and wbr, plus the
obsolete basefont, bgsound, frame and keygen the HTML
parser still treats as such - which hold no content at all. The component knows them, so it renders them
without a closing tag and leaves the ChildContent out rather than emitting markup a browser would have
to repair.
Attributes & events
href and target on an anchor, placeholder
on an input, a data-* attribute anywhere, and the Blazor event handlers (@onclick,
@oninput, ...) that go with them. Nothing about them is tag-specific, so the same markup keeps
working when the tag changes.
Event modifiers
@onclick:stopPropagation and @onclick:preventDefault
directives on a plain HTML element, never on a component, so the element offers them as parameters instead.
StopPropagation keeps a click inside a clickable container from also reaching that container, and
PreventDefault cancels the browser's own action - the navigation of an anchor, the submit of a button -
while the click handler still runs. StopPropagationEvents and PreventDefaultEvents are the same
two modifiers for every other event, named with or without the on prefix: the
contextmenu a custom menu replaces, the dragover of a drop target, the
submit of a form, a keydown the page handles itself. A name in either list is a
modifier of the element rather than a handler of the event, so it applies whether or not a handler of that
event is written beside it - and naming the click in a list is allowed too, where it has the
last word over the parameter of the same name beside it.
Disabled
bit-dis
class, so the state means the same thing whichever tag is rendered. On top of that it writes the
disabled attribute only on the elements HTML defines one on (button,
fieldset, input, optgroup, option, select and
textarea), and on every other tag a tabindex of -1 instead, so that a tag the browser
will not disable of itself is at least out of the keyboard's reach as well as the pointer's.
aria-disabled goes on all of them, so assistive technologies announce the element as disabled
rather than as missing. A disabled hyperlink - an a or an image map's area - also
loses its href, since a link is focusable and followed by the enter key whatever the tab order
says. The state describes the element itself: content inside it keeps its own tab stops.
Visibility
No wrapper
if. Since no element is rendered, everything that describes one - the tag, the class, the style,
the id, the direction, the splatted attributes and the captured element reference - has nowhere to land and is
ignored.
Dynamic element
href on the anchor, placeholder on the input and the textarea - which is
what lets one piece of markup serve every one of them.
Element reference
@ref on the component reaches the rendered element: FocusAsync gives it the browser
focus, and RootElement is the ElementReference to hand to JavaScript interop. The
element has to be one the browser can focus - a tag that is focusable of itself, or any other tag carrying a
TabIndex, which is what puts the div below into the tab order. The overload of
FocusAsync taking a preventScroll flag focuses without the browser scrolling the
document to bring the element into view. Both are about an element that is there, so while NoWrapper
is set, or before the first render, the call does nothing rather than fail.
Style & Class
style or a class written as a plain HTML attribute
rather than replacing it, so an element can take a class from a parameter and another one from splatted
attributes at the same time and keep both.
RTL
dir attribute and, for
right-to-left, the bit-rtl class the rest of the library reads. It applies to whichever tag is
rendered and cascades to everything inside it.
یک نقل قول راستچین.
API
Every parameter, public member, sub-class and enum this component exposes.
BitElement parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| ChildContent | RenderFragment? | null | The content of the element. A void element (such as input, img, br or hr) holds no content, so it is not rendered into one. |
| Element | string? | null | The custom html element used for the root node. Any tag name is accepted, including SVG and custom elements, and it is used exactly as written. A value that is not a name a tag can be made of - a letter followed by letters, digits and the "-", "_", "." and ":" that join them - falls back to the default, which is "div". |
| NoWrapper | bool | false | Renders only the content of the element, without the wrapping HTML tag, which makes the component a conditional wrapper. Everything that describes the element itself is then ignored, apart from a Collapsed Visibility, which drops the content as well. |
| PreventDefault | bool | false | Prevents the default browser action of the click event of the element, which is the @onclick:preventDefault directive Razor only accepts on a plain HTML element. |
| PreventDefaultEvents | IEnumerable<string>? | null | The names of the events whose default browser action is prevented on the element, with or without the "on" prefix. This is PreventDefault for every event other than the click, and naming the click here has the last word over that parameter. |
| StopPropagation | bool | false | Stops the click event of the element from bubbling up to its ancestors, which is the @onclick:stopPropagation directive Razor only accepts on a plain HTML element. |
| StopPropagationEvents | IEnumerable<string>? | null | The names of the events that are stopped from bubbling up from the element to its ancestors, with or without the "on" prefix. This is StopPropagation for every event other than the click, and naming the click here has the last word over that parameter. |
BitElement public members
| Name | Type | Default value | Description |
|---|---|---|---|
| FocusAsync | ValueTask | Gives the browser focus to the rendered element, which has to be one the browser can focus: a tag that is focusable of itself, or any other tag carrying a TabIndex. The overload taking a preventScroll flag focuses it without the browser scrolling the document to bring it into view. Nothing is rendered while NoWrapper is set and nothing is captured before the first render, so there the call does nothing rather than fail. |
BitComponentBase parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| AriaLabel | string? | null | Gets or sets the accessible label for the component, used by assistive technologies. |
| Class | string? | null | Gets or sets the CSS class name(s) to apply to the rendered element. |
| Dir | BitDir? | null | Gets or sets the text directionality for the component's content. |
| ForceAnimation | bool | false | Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested. |
| HtmlAttributes | Dictionary<string, object> | new Dictionary<string, object>() | Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. |
| Id | string? | null | Gets or sets the unique identifier for the component's root element. |
| IsEnabled | bool | true | Gets or sets a value indicating whether the component is enabled and can respond to user interaction. |
| Style | string? | null | Gets or sets the CSS style string to apply to the rendered element. |
| TabIndex | string? | null | Gets or sets the tab order index for the component when navigating with the keyboard. |
| Visibility | BitVisibility | BitVisibility.Visible | Gets or sets the visibility state (visible, hidden, or collapsed) of the component. |
BitComponentBase public members
| Name | Type | Default value | Description |
|---|---|---|---|
| UniqueId | Guid | Guid.NewGuid() | Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed. |
| RootElement | ElementReference | Gets the reference to the root HTML element associated with this component. |
BitVisibility enum
| Name | Value | Description |
|---|---|---|
| Visible | 0 | The content of the component is visible. |
| Hidden | 1 | The content of the component is hidden, but the space it takes on the page remains (visibility:hidden). |
| Collapsed | 2 | The component is hidden (display:none). |
BitDir enum
| Name | Value | Description |
|---|---|---|
| Ltr | 0 | Ltr (left to right) is to be used for languages that are written from the left to the right (like English). |
| Rtl | 1 | Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic). |
| Auto | 2 | Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element. |
Feedback
Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.