DevactionUI.CodeBlock (devaction_ui v0.2.3)

Copy Markdown View Source

A block of code in mono, given as the code attribute. lang: :heex colours it (components, slots, attributes, the special :if/:for/phx-* ones, strings, expressions, comments) and lang: :elixir colours Elixir, both through DevactionUI.Highlight; the code inside a HEEx {} or <% %> is coloured as Elixir too. tone: :dark is the ink block of a suggestion; :risk and :ok are the "your code" and "idiomatic" blocks of a lesson, tinted red and green; :neutral is the quiet tint. The dark block has fixed colours on purpose: it is a dark surface in both themes, and so is its syntax palette (the tinted tones take theirs from the tone). It keeps its own line breaks and scrolls sideways instead of widening the page.

lang knows :heex and :elixir; any other code stays plain. With a label the block is a named region that the keyboard can focus, so a long line can be scrolled without a mouse. With copy_label a button copies the code (the DevactionUI.CodeCopy hook, so it needs an id; it stays hidden where the browser cannot write to the clipboard) and says copied_label for a moment.

Lines. line_numbers draws a number before each line (generated by CSS, so copying the code does not copy them), highlight_lines marks lines by number or range ([3, 5..7]) and diff reads the first character of each line as a diff marker: + is an added line and - a removed one (the marker stays in the text, so the difference is not only a colour). The three work together and with lang.

class and rest go to the <pre>, not to the container; id goes to the container.

Summary

Functions

code_block(assigns)

Attributes

  • tone (:atom) - :dark a suggestion, :risk the code to avoid, :ok the idiomatic one, :neutral quiet. Defaults to :dark. Must be one of :dark, :risk, :ok, or :neutral.
  • id (:string) - Defaults to nil.
  • label (:string) - a mono caption above the block, and the name of its region for assistive technology. Defaults to nil.
  • copy_label (:string) - the name of the copy button; without it, no button. Defaults to nil.
  • copied_label (:string) - said for a moment after the copy. Defaults to nil.
  • lang (:atom) - :heex colours the code by what each part of the template is, :elixir colours Elixir. Defaults to nil. Must be one of nil, :heex, or :elixir.
  • line_numbers (:boolean) - a number before each line. Defaults to false.
  • highlight_lines (:list) - lines to mark, by number or range: [3, 5..7]. Defaults to [].
  • diff (:boolean) - lines that start with + are added, with - removed. Defaults to false.
  • class (:any) - Defaults to nil.
  • code (:string) (required) - the code as text: a pre keeps every space, so it never comes from a slot.
  • Global attributes are accepted.

Slots

  • copy_icon - the icon of the copy button; without it the button shows copy_label.