Lovelace Expander Card

repository·master·Indexed 19 days ago

https://github.com/alia5/lovelace-expander-card

A custom Home Assistant card that creates expandable and collapsible sections in dashboards. It supports nesting expanders, custom title cards, and background clearing for a seamless UI. Key features include the ability to persist expansion state via expand-id using browser localStorage and the option to replace standard text titles with full Lovelace card configurations.

Tokens
2.2K
Snippets
7
Records
9
Agent score
62%

What's inside lovelace-expander-card

  1. Install the Lovelace Expander Card via HACS

    master

    The easiest way to install the expander card is through the Home Assistant Community Store (HACS). You can use the direct HACS integration link to add the repository to your sources and open the integration automatically.

    Alternatively, you can add the repository manually in HACS by following the HACS custom repositories documentation using the repository URL: https://github.com/Alia5/lovelace-expander-card

  2. Install the Lovelace Expander Card manually

    master

    To install the card without HACS, follow these two steps:

    1. Download the card

    Download the lovelace-expander-card.js file and place it in your Home Assistant <config directory>/www/ folder.

    Add the resource to your ui-lovelace.yaml file so Home Assistant knows where to find the JavaScript file.

    Note: The /www/ directory in Home Assistant is mapped to the /local/ URL path.

    # 1. Download the card
    ```bash
    wget https://github.com/Alia5/lovelace-expander-card/releases/download/latest/lovelace-expander-card.js
    mv lovelace-expander-card.js /config/www/

    2. Link the card to your lovelace ui

    resources:
      - url: /local/lovelace-expander-card.js
        type: js
  3. Use `expand-id` to persist expansion state

    master

    The expander-card can remember whether it was expanded or collapsed across browser sessions using the expand-id configuration key.

    When an expand-id is provided, the card's state (expanded or collapsed) is stored in the browser's localStorage under the key expander-{expand-id}. This allows the user's preferred view to persist when they refresh the page or return to the dashboard.

    # Example of persisting state with a unique ID
    type: custom:expander-card
    expand-id: "living_room_lights"
  4. Configure the Expander Card default options

    master

    The expander-card uses several configuration keys to control its visual layout and behavior. If not explicitly provided in your Home Assistant dashboard configuration, the following default values are applied:

    KeyDefault ValueDescription
    gap0.6emThe spacing between child cards.
    padding1emThe padding of the main expander card.
    clearfalseIf true, the card background becomes transparent and borders are removed.
    clear-childrenfalseIf true, child cards will have transparent backgrounds and no borders.
    titleExpanderThe text displayed in the header if no title-card is provided.
    overlay-margin2emThe margin for the toggle button when using title-card-button-overlay.
    child-padding0.5emThe padding applied to the container of the child cards.
    button-backgroundtransparentThe background color of the header toggle button.
    {
      "type": "custom:expander-card",
      "title": "My Expander",
      "gap": "1em",
      "padding": "2em",
      "clear": true,
      "clear-children": true
    }
  5. Configure a custom title card

    master

    Instead of a simple text title, you can use the title-card option to render a full Home Assistant card as the header of the expander. This allows for more complex headers (e.g., showing a sensor value or a specific button).

    Related options for custom title cards:

    • title-card: The configuration object for the card used as the header.
    • title-card-button-overlay: If true, the toggle button is positioned as an overlay on the title card.
    • title-card-padding: Custom padding for the title card container.
    • title-card-button-overlay (via overlay-margin): Controls the margin of the toggle button when in overlay mode.
    type: custom:expander-card
    title-card:
      type: sensor
      entity: sensor.temperature
    title-card-button-overlay: true
      overlay-margin: 1em
  6. Configure the Lovelace Expander Card

    master

    The ExpanderConfig interface defines the YAML configuration options available for the Lovelace Expander Card in Home Assistant. Use these keys to control the layout, styling, and behavior of the expander and its children.

    type: custom:lovelace-expander-card
    title: "My Expander"
    gap: "10px"
    padding: "10px"
    'button-background': "#ff0000"
    # Optional settings
    expanded: true
    'title-card':
      type: markdown
      content: "Custom Title"
    cards:
      - type: button
        name: "Child Card"
  7. Configure the Lovelace Expander Card options

    master

    The expander card is configured using YAML or the graphical editor. Below is the reference for all available configuration keys.

    NameTypeDefaultDescription
    typestringRequiredMust be custom:expander-card
    titlestringExpanderTitle of the card (not displayed if using title-card)
    clearbooleanfalseRemove Background
    clear-childrenbooleanfalseRemove Backgrounds/Borders of child cards
    expandedbooleanfalseStart in the expanded state
    expand-idstringoptionalUnique ID for JS LocalStorage to save the expanded state
    button-backgroundstringtransparentCSS color for the expand button background
    gapstring0.6emCSS size for the gap between child cards
    paddingstring1emCSS size for padding of all card content
    child-paddingstring0.5emCSS size for padding of child cards
    title-cardobjectoptionalReplace Title with a custom Lovelace card configuration
    title-card-paddingstring0pxCSS size for padding of the title-card
    title-card-button-overlaybooleanfalseOverlay the expand button over the title-card
    overlay-marginstring2emCSS size for margin from top right of the button (if overlay)
    cardsobject[]optionalArray of child cards to show when expanded
  8. Register the Expander Card with Home Assistant

    master

    The lovelace-expander-card package exports the ExpanderCard.svelte component as the default export. This component is used by Home Assistant to render the expander card UI. When installed, the card registers itself on the window.customCards array, allowing Home Assistant to identify the card type and metadata.

    import ExpanderCard from './ExpanderCard.svelte';
  9. Reference: ExpanderCard configuration options

    master

    The following configuration keys are available for the lovelace-expander-card:

    # Layout and Spacing
    gap: string             # Spacing between elements
    padding: string         # Padding for the card
    'child-padding'?: string # Padding for child cards
    'title-card-padding'?: string
    'overlay-margin'?: string
    
    # Content and Structure
    title: string            # The main title text
    cards?: { type: string }[] # List of child card configurations
    'title-card'?: LovelaceCardConfig # A custom card to use as the title
    
    # Behavior and State
    expanded?: boolean        # Initial expanded state
    'expand-id'?: string     # ID for expansion control
    clear?: boolean          # Clear state
    'clear-children'?: boolean # Clear children state
    
    # Styling
    'button-background': string # Background color for the expander button
    'title-card-button-overlay'?: false