Code
v0.0.10
The Mishka Chelekom Mob Code component shows inline code and code blocks in a monospace font on iOS and Android. A block scrolls horizontally instead of wrapping. It is the native port of the headless Code and runs on Mob.
Overview of the Mob Code component for iOS and Android
Code is a pure Elixir Mob composite. You write it as a <MishkaCode> tag and pass the code in text. It sets the font of Mob's Text to "monospace", which falls back to the platform's own monospace face (Roboto Mono on Android, SF Mono on iOS).
Code has two modes. Inline code sits in a sentence and hugs its text, with no scrolling. A block, which you get with block={true}, fills the width, is padded, and scrolls horizontally, because code lines do not wrap and a long line would otherwise be cut off with no way to read the rest.
Code has no state and sends no events. It renders the text you pass.
Generate the code component
Run this from your Mob app's root to add the component to lib/<app>/components/:
mix mishka.ui.gen.mob codeThe generator registers the component's tag, so you can use it in ~MOB markup. The examples on this page use the mishka_ prefix of the showcase app (--module-prefix mishka_), so the tag is <MishkaCode>. Without a prefix the tag is <Code>.
Inline code
Inline code hugs its text and sits in a sentence.
Inline code
<MishkaCode text="mix mob.deploy" />Code block
A block fills the width, is padded, and scrolls horizontally. Code does not wrap.
Code block
<MishkaCode text="…" block={true} />Code block without the scroller
Set scroll={false} and a long line is clipped at the edge of the block instead of scrolling.
Code block without the scroller
<MishkaCode text="…" block={true} scroll={false} />Code block colors
background and color are props, so you can draw a dark block in any theme.
Code block colors
<MishkaCode text="…" block={true} background={0xFF0B1020} color={0xFF93C5FD} />Props
Props of the <MishkaCode> tag.
| Prop | Type | Default | Description |
|---|---|---|---|
text | string | nil | The code. |
block | boolean | false | Renders a block instead of inline code. |
background | color / ARGB | :surface_raised | Fill. |
color | color / ARGB | :on_surface | Code color. |
text_size | size token | :sm | Code size. |
padding | spacing / number | :space_md for blocks, 4 for inline | Padding. Blocks and inline code use different defaults. |
scroll | boolean | true for blocks | Scrolls a block horizontally instead of clipping it. |
id | string | None | A native test tag, for finding a block whose text is long. It is a test handle, not a DOM id. |
Differences from the web component
These parts of the web component are not ported or work differently:
- The
*_classattributes are not ported. idis ported as a native test tag, not as a DOM id.- Syntax highlighting is not part of the headless component either. A native version would need per-token spans, which Mob's
Textdoes not expose.
Attributes
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
text
|
string
| — | — | No | The code. |
block
|
boolean
|
false
| — | No | Block rather than inline. |
background
|
color token / ARGB int
|
:surface_raised
| — | No | Fill. |
color
|
color token / ARGB int
|
:on_surface
| — | No | Code colour. |
text_size
|
size token
|
:sm
| — | No | Code size. |
padding
|
spacing token / number
|
:space_md (block) / 4 (inline)
| — | No | Padding. |
scroll
|
boolean
|
true for blocks
| — | No | Scroll a block horizontally. |
Options
Each option takes one or more of its values, separated by commas.
| Option | Values |
|---|---|
--type
|
code
|
--module
| A module name of your own |
What it generates
mix mishka.ui.gen.mob code