MonsterUI Documentation

repository·main·Indexed 19 days ago

https://github.com/answerdotai/monsterui

A UI framework for FastHTML that combines Python with Tailwind CSS via Franken UI and DaisyUI to build web interfaces with minimal code. It provides a comprehensive set of components, including Grid and Flexbox helpers (DivHStacked, DivVStacked, DivFullySpaced), theme configuration via the Theme enum, and DaisyUI components such as Alert, Steps, Loading indicators, and Toasts.

Tokens
20.1K
Snippets
82
Records
93
Agent score
67%

What's inside MonsterUI

  1. Access LLM context files and Markdown representations

    main

    MonsterUI provides specialized files to assist LLM-based development. You can access documentation and API lists via specific URLs. Additionally, you can append suffixes to your application URLs to get different representations of your content:

    • Add /md to a URL to get a markdown representation.
    • Add /rmd to a URL to get a rendered markdown representation.
  2. Quickstart: Create a MonsterUI app

    main

    To create a basic MonsterUI application, import the necessary modules, select a theme color, and initialize the app using fast_app with the theme's headers. You can then define routes using standard FastHTML patterns and use MonsterUI components like Card, Titled, and UkIconLink to build your UI.

    from fasthtml.common import *
    from monsterui.all import *
    
    # Choose a theme color (blue, green, red, etc)
    hdrs = Theme.blue.headers()
    
    # Create your app with the theme
    app, rt = fast_app(hdrs=hdrs)
    
    @rt
    def index():
        socials = (('github','https://github.com/AnswerDotAI/MonsterUI'),
                   ('twitter','https://twitter.com/isaac_flath/'),
                   ('linkedin','https://www.linkedin.com/in/isaacflath/'))
        return Titled("Your First App",
            Card(
                H1("Welcome!"),
                P("Your first MonsterUI app", cls=TextPresets.muted_sm),
                P("I'm excited to see what you build with MonsterUI!"),
                footer=DivLAligned(*[UkIconLink(icon,href=url) for icon,url in socials])))
    
    serve()
  3. Install MonsterUI via pip

    main

    Install the MonsterUI library using pip to add Tailwind-based UI components (Franken UI and DaisyUI), Markdown support (Mistletoe), code highlighting (HighlightJS), and LaTeX support (Katex) to your FastHTML applications.

    pip install MonsterUI
  4. Understand MonsterUI spacing abbreviations

    main

    MonsterUI follows Tailwind CSS naming conventions for spacing. Use these abbreviations in the cls parameter of components to control layout:

    • Axes: t (top), b (bottom), l (left), r (right), x (horizontal/both sides), y (vertical/both sides).
    • Types: p (padding), m (margin), space (spacing between elements).

    Examples:

    • mt: margin-top
    • px: padding on both left and right
    • space-y: apply spacing on the y-axis (top and bottom) between children.
  5. How Theme and headers work together

    main

    MonsterUI uses a Theme enum to encapsulate the entire visual configuration of an application. Instead of manually managing CSS links and JS scripts, you select a Theme (e.g., Theme.blue) and call its .headers() method.

    This method performs several tasks:

    1. Loads Core CSS/JS: Injects FrankenUI and Tailwind CSS.
    2. Applies Theme Identity: Injects a script that applies the specific color theme, radii, shadows, and font settings to the <html> element.
    3. Configures Plugins: Optionally loads DaisyUI, syntax highlighting (Highlight.js), math rendering (KaTeX), or charts (ApexCharts) based on your boolean flags.
    4. Handles Dark Mode: Sets up a MutationObserver so that if the user toggles dark mode, the syntax highlighting themes and other UI elements update automatically.
  6. Use Accordion and AccordionItem components

    main

    The Accordion component allows for collapsible content sections.

    • AccordionItem(title, *c, ...): A single item within an accordion. It handles the title, the expandable content (*c), and the open state.
    • Accordion(*c, ...): The container for one or more AccordionItem components.

    Accordion Options:

    • multiple: (bool) Allow multiple items to be open simultaneously.
    • collapsible: (bool) Allow all items to be closed.
    • animation: (bool) Enable/disable animation.
    • duration: (int) Animation duration in ms.
    • active: (int) 0-based index of the item to be open by default.
    • tag: (str) HTML tag for the container ('ul' or 'div').
    from monsterui.franken import Accordion, AccordionItem
    
    accordion = Accordion(
        AccordionItem("Section 1", "Content for section 1"),
        AccordionItem("Section 2", "Content for section 2", open=True),
        multiple=True
    )
  7. Use Grid for predictable layouts

    main

    The Grid component is best suited for regular, predictable layouts where elements have similar shapes. By default, grid items are evenly sized. You can control the size of specific elements using row-span-{int} and col-span-{int} classes to make them occupy multiple rows or columns.

    # Simple grid of cards
    Grid(*[Card(picsum_img(i), P(f"Image {i}")) for i in range(6)])
    
    # Grid with spanning elements
    Container(Grid(sidebar, *stats, *chart_cards, cols=5))
  8. Distinguish between Padding and Margin

    main

    Use Margin to move a component relative to its surroundings without changing its internal size. Use Padding to increase the space inside a component's container.

    • ml-20: Applies margin to the left of the component (outside the container).
    • pl-20: Applies padding to the left inside the component (inside the container).
    Grid(
        Card(H4("A Simple Card with ml-20", style='background-color: red'), cls='ml-20'), 
        Card(H4("A Simple Card with pl-20", style='background-color: red'), cls='pl-20')
    )
  9. Use Flexbox helpers for alignment

    main

    MonsterUI provides several helper functions to simplify common Flexbox patterns without requiring deep knowledge of CSS flex properties:

    • DivHStacked: Aligns children horizontally in a row, centers them vertically, and adds spacing (cls=(FlexT.block, FlexT.row, FlexT.middle, 'space-x-4')). Useful for forms (label next to input) or placing text next to an avatar.
    • DivVStacked: Aligns children vertically in a column and centers them (cls=(FlexT.block, FlexT.column, FlexT.middle)). Useful for stacking text elements or centering content within a card.
    • DivFullySpaced: Uses flex to distribute space between items as much as possible. Ideal for headers or footers where you want elements pushed to opposite ends.
    # Horizontal stacking (e.g., Avatar + Text)
    DivHStacked(
        DiceBearAvatar("user"), 
        DivVStacked(
            P("John Doe", cls=TextT.lg),
            P("john@example.com", cls=TextT.muted)
        )
    )
    
    # Vertical stacking (e.g., Pricing Card header)
    DivVStacked(
        H2(plan),
        H3(price, cls='text-primary'),
        P('per month', cls=TextT.muted),
        cls='space-y-1'
    )
  10. Configure themes and headers using the Theme class

    main

    The Theme enum is the primary way to select a visual style for your application. Each theme member provides a .headers() method that returns a list of HTML elements (links, scripts, styles) required to load the necessary CSS and JS (FrankenUI, Tailwind, DaisyUI, etc.).

    Available Themes

    Available theme colors include:

    • Theme.slate, Theme.stone, Theme.gray, Theme.neutral
    • Theme.red, Theme.rose, Theme.orange, Theme.green, Theme.blue, Theme.yellow, Theme.violet, Theme.zinc

    Customizing Headers

    You can customize the look and feel by passing arguments to .headers():

    • mode: Set to 'auto', 'light', or 'dark' to control color mode.
    • radii: Use ThemeRadii (e.g., ThemeRadii.sm, ThemeRadii.md) to set corner rounding.
    • shadows: Use ThemeShadows (e.g., ThemeShadows.sm, ThemeShadows.lg) to set shadow depth.
    • font: Use ThemeFont (e.g., ThemeFont.sm, ThemeFont.default) to set font size.
    • icons: Boolean, defaults to True (loads FrankenUI icons).
    • daisy: Boolean, defaults to True (loads DaisyUI styles).
    • highlightjs: Boolean, defaults to False. Set to True for syntax highlighting.
    • katex: Boolean, defaults to False. Set to True for math rendering.
    • apex_charts: Boolean, defaults to False. Set to True for charting support.
    app = FastHTML(hdrs=Theme.blue.headers(highlightjs=True, katex=True))
  11. Use Gap vs Spacing for component layout

    main

    Both gap and space manage the distance between components, but they behave differently:

    Gap

    • Behavior: Creates a gap between every element.
    • Best Use Case: Use gap when working with Grid or Flexbox elements. It ensures consistent spacing between all items.
    • Example: cls='gap-4'

    Spacing

    • Behavior: Applies margin to every element in a group except for the first element.
    • Best Use Case: Use space (e.g., space-y-5) for vertical stacks like Form components where you want space between inputs but do not want extra space above the first element (like a heading).
    • Example: cls='space-y-5'

    Note: gap will not work on standard block elements that are not part of a flex or grid layout; in those cases, use space.