šŸ”¹ Card-mod - Super-charge your themes!

yes, thats why I have this in my resources.yaml:

# TAKE CARE!! MANUAL CHANGES IN SOURCECODE
# https://github.com/thomasloven/lovelace-fold-entity-row
- url: /local/resources/fold-entity-row/fold-entity-row.js?v=2.2.0
# - url: /hacsfiles/lovelace-fold-entity-row/fold-entity-row.js
  type: module

since I use yaml, I have to edit those lines anyways, and I will be reminded every time it needs updating (happens not that oftenā€¦)

indeed.

maybe even nicer if we could set some of these values in a global config on the resource itself.

given thats been 2 years now, I take it Thomas isnt interested in adding that :wink:

so, still hope for card-mod-themes.

No need to have 100500 different cards.
Try to unify all you views, then select patterns which can cover all your use-cases - and for these patterns create decluttering cards with all needed mods. Means - edit a code in 1 place ā†’ get results in many.
Very same about automations (blueprints).

Edit: at some point people create configs simpler because some mods/features are not really needed IRL.

ofc I have considered those decluttering templates, and use them where I can. itā€™s a nice tool I use throughout the config
However, many of these fold-entity-rows in my config have extra mods, for top margin, for scrolling etc etc.
Also, they dont all list the same items. Those items need their own mods, like when listing entities, or maps, or graphsā€¦ all need specific additional mods.

so no way of creating a generic decluttering I am afraid.

unless we could ā€˜mergeā€™ the mods, like we can do in button-cardā€¦ maybe I should revisit that once and see what gives.

I have several decluttering folds, which are used when eg listing identical sensors, in this case my doors:

card:
  type: custom:fold-entity-row
#   card_mod: #!secret style_toggle_icon
#     style:
#       ha-icon:
#         $: |
#           ha-svg-icon {
#             color: var(--primary-color);
#             width: 24px;
#           }
  head:
    type: section
    label: '[[name]] deur'
    card_mod: #!secret style_label_margin
      style: |
        .label {
          margin-left: 0px !important;
        }
  padding: 0
  entities:
    - entity: binary_sensor.[[id]]_door
      secondary_info: last-changed
    - entity: sensor.[[id]]_door_battery_level
    - entity: binary_sensor.[[id]]_door_low_battery_level

this is by no means generic, and only used to cut down on 14 doors codeā€¦

Yes, I also find that everytime I use card_mod on a card it is slightly different. So I never started using the decluttering card. It would probably make sense to check it out again but I am finding that IF I need to mod, it is usually because layouts donā€™t fit. And those are usually a problem specific to the one card I am building.
I.e., each of those (usually grid) cards contains a different combination of cards and I have to mod each one for it to fit properly inside the grid.

What would be cool would be if anchors (I think that is what the style refs are called) were not changed to the referenced code but stayed as anchors on saving. That would save some code. But making a decluttering template for 5 instances is maybe more work than repeating the code via c&p in that one card.

Right now I am thinking of removing my themed padding and reverting to per-card-padding. Maybe that would be a good use case for decluttering. But I am not sure if that will really save me muchā€¦

I just checked and I have a total of ā€œ10 cardsā€ in my main dashboard (each card is usually a templated card or grid with subcards). I try to have as little cards as possible with as much information and features as possible because mobile phones are simply not made for card rich dashboards.

I did give that another try with this generic decluttering-card using all mods I need:

card:
  type: custom:fold-entity-row
  card_mod:
    style:
      ha-icon:
        $: |
          ha-svg-icon {
            color: red;
            width: 24px;
          }

  head:
    type: section
    label: '[[label]]'
    card_mod:
      style: |
        .label {
          margin-left: 0px !important;
        }
  padding: 0
  entities:
    '[[entities]]'

and it works 50% ok.

the mods for the fold icon (size and color) dont work, even though setting these on a fold-entity-row card directly do what they should. No effect at all.

the mods in the head do work as expectedā€¦

maybe a bug in decluttering-card, maybe a user issue?

for now, it seems I still need to edit the icon size and color inside the resource itself (has the benefit of the auto repositioning using max-width: calc(100% - var(--toggle-icon-width) - 8px);

and I can use the decluttering for the other mods.
Or, try and find how to edit the resource to fix padding and label

  head:
    type: section
    label: '[[label]]'
    card_mod:
      style: |
        .label {
          margin-left: 0px !important;
        }
  padding: 0

which should be doable, easy even, but I am too daft to findā€¦

Do you need card_mod in there? I thought decluttering-card could modify the icon directly.

decluttering_templates:
  my_first_template:     # This is the name of a template
    default:
      - icon: fire
    card:
      type: custom:button-card
      name: '[[name]]'
      icon: 'mdi:[[icon]]'

But I am just asking without being too familiar with it. Just what I concluded from the examples in the decluttering-card thread.

m0: |
          [[[
            if ('[[m0_show]]' == 'true') {
# parts omitted for size reasons
              }
              return `<ha-icon icon=${icon}
              style="width: 14px; height: 14px; color: ${icon_color};">
              </ha-icon><span style="color: white";> ${status}</span>`
            }
            return ``
          ]]]

I would like to move part of a recurring card mod to a theme but I could not find a matching example here and my conclusion from a different example seems to be false.

            entities:
              - entity: input_text.meals_1
                card_mod:
                  style:
                    ha-textfield $: |
                      .mdc-text-field {
                        height: 3em !important;
                      }
                      .mdc-text-field__input {
                        align-self: center !important;
                      }
                      span#label {
                        display: none !important;
                      } 

I thought would translate to

        ha-textfield .mdc-text-field {
          height: 3em !important;
        }
        ha-textfield .mdc-text-field__input {
          align-self: center !important;
        }
        ha-textfield span#label {
          display: none !important;
        } 

So first level .second level {}

I am successfully using this for

        hui-generic-entity-row .info.text-content {
          overflow: visible;
          margin-left: 0px;
          margin-right: 0px;
        }

Where am I going wrong now with the ha-textfield?

had to change the footer chip a lot, and still am not finished:

    card-mod-card-yaml: |
      hui-buttons-header-footer $ hui-buttons-base:
        $ : |
          .ha-scrollbar {
            justify-content: space-evenly !important;
            height: 50px;
            align-content: center !important;
            margin: -8px 0px 0px 0px;
          }

      hui-buttons-header-footer $ hui-buttons-base $: |
          ha-assist-chip {
            border: 1px solid green;
            border-radius: var(--ha-card-border-radius);
            --ha-assist-chip-filled-container-color: var(--primary-color);
          }

now shows as

but as you can see, the icon and the text are still not styled.

before the migration to these new elements, I had set the ripple, and styles on chip-text color, secondary color and icon:

  card-mod-card-yaml: |
    hui-buttons-header-footer $ hui-buttons-base $ : |
      .ha-scrollbar {
        justify-content: space-evenly;
        height: 50px;
        align-content: center;
        margin: -8px 0px 0px 0px;
        --ha-chip-background-color: var(--primary-color);
        --ha-chip-text-color: var(--card-background-color);
        --paper-item-icon-active-color: red;
        --secondary-text-color: var(--card-background-color);
      }

    hui-buttons-header-footer $ hui-buttons-base $ .ha-scrollbar ha-chip:
      $: |
        .mdc-chip {
          border: 1px solid green;
          border-radius: var(--ha-card-border-radius);
        }
        .mdc-chip__ripple {
          border-radius: var(--ha-card-border-radius) !important;
        }

seems we can no longer touch those? because even when using

            --ha-chip-text-color: red !important;
            --paper-item-icon-color: red !important;

which are recognized in Inspector:

SchermĀ­afbeelding 2024-01-15 om 15.56.14

they are not applied in the chips?

btw, checking the source for these ha-assist-chips would confirm that there are no available variables for those colors??

answering myself here, I do believe I found some workin variable, unexpected if they areā€¦

      hui-buttons-header-footer $ hui-buttons-base $: |
          ha-assist-chip {
            border: 1px solid var(--primary-color);
            border-radius: var(--ha-card-border-radius);
            --secondary-text-color: var(--primary-color);
            --primary-text-color: var(--primary-color);
          }

where secondary text color pertains to the icon, and primary text to the labelā€¦

need some space between the icon and the label and then weā€™re set for nowā€¦
finally we are getting somewhere:

nice we can set the text weight with yet another variable family name:

            --_label-text-weight: bold;

man this is truly unmaintainable.

final try for today, I can only manipulate the space between icon and label using:

    card-mod-card-yaml: |
      hui-buttons-header-footer $ hui-buttons-base:
        $ : |
          .ha-scrollbar {
            justify-content: space-evenly;
            height: 50px;
            align-content: center;
            margin: -8px 0px 0px 0px;
          }

      hui-buttons-header-footer $ hui-buttons-base $: |
          ha-assist-chip {
            border: 1px solid var(--primary-color);
            border-radius: var(--ha-card-border-radius);
            --secondary-text-color: var(--primary-color);
            --primary-text-color: var(--primary-color);
            --_label-text-weight: bold;
          }
          ha-assist-chip state-badge {
            justify-content: space-between;
          }

but it is unbalanced on the full button itselfā€¦

What do you mean it is unbalanced?
The screenshot looks like you move the button out of its frame (I am guessing because of your margin -8px).
But I am not sure what else looks unbalanced. It looks like the icon remains where it was but the text looks like it is aligned left rather than center. But hard to tell without seeing the inspector frames.

Maybe I should add that I have been testing this in card-mod-row: . But I also tried card-mod-card. Also without success.
Where am I going wrong?

I canā€™t imagine that textfield is a <thing>, i.e. card-mod-textfield. At least it did not work when I tried

      card-mod-textfield: |  
        .mdc-text-field {
          height: 3em !important;
        }
        .mdc-text-field__input {
          align-self: center !important;
        }
        span#label {
          display: none !important;
        }

So presumably not a <thing>.

I am sure it must be a simple solution but for the life of me I cannot think of anything else to try that I have not tried.

card-mod-row: |
  ha-textfield .mdc-text-field {
     height: 3em !important;
  }

card-mod-card: |
  ha-textfield .mdc-text-field {
     height: 3em !important;
  }

card-mod-row: |
  .mdc-text-field {
     height: 3em !important;
  }

card-mod-card: |
  .mdc-text-field {
     height: 3em !important;
  }

Frustrating to have the working code for the card_mod of a card but not get it working in the theme -.-

No, the -8 is for correcting the vertical space inside the footer

I meant the button itself where the icon and the label are not evenly spaced or, have different spacing left and right

Tbh, we have no idea what is ā€˜not workingā€™ in your theme mod.

There is screen nor description?

I posted the code and thought that would be what is needed.
But very happy to share everything I can.
So, this is what it is supposed to look like:

It is achieved with the following card_mod:

Summary
type: entities
entities:
  - entity: input_text.fancy_meals_2
    card_mod:
      style:                
        ha-textfield $: |
          .mdc-text-field {
            height: 3em !important;
          }
          .mdc-text-field__input {
            align-self: center !important;
          }
          span#label {
            display: none !important;
          }               

This is what it looks like without the card_mod:

Maybe a bit more difficult to see, but the text-field is much higher (missing height: 3em !important;) and the text is at the bottom instead of the center (missing align-self: center !important;).

There are some other theme mods in play that already make the icon smaller and remove the fieldā€™s label. But they are in :host.

So I would like to add

    card_mod:
      style:                
        ha-textfield $: |
          .mdc-text-field {
            height: 3em !important;
          }
          .mdc-text-field__input {
            align-self: center !important;
          }
          span#label {
            display: none !important;
          }   

to my theme.
I thought it would translate to

      card-mod-row: |
        ha-textfield .mdc-text-field {
          height: 3em;
        }
        ha-textfield .mdc-text-field__input {
          align-self: center;
        }
        ha-textfield span#label {
          display: none;
        }

but it does not work, i.e. the mod is not being applied. Neither for card-mod-card nor card-mod-row.

So I guess my main problem is finding where the ha-textfield is.
Even tried card-mod-textfield as the card-mod-<thing>.

so, you have this working in a regular card mod?

Why use the theme for that then,

or do you have a lot of those inputs?

I believe I have 1, let me try and find it for you

(sorry for this ridiculous post, I keep getting those incorrect format warningsā€¦)

edit

here it is ( did hide the icon though, to make it replace the custom card I used for this before:

      - entity: input_text.message
        name: Bericht
        <<: &no_icon
          card_mod:
            style:
              hui-generic-entity-row $: |
                state-badge {
                  display: none;
                }
              ha-textfield:
                $: |
                  #label {
                    left: 0px !important;
                  }
              .: |
                :host {
                  --text-field-padding: 0px;
                }

I have it working in a regular card with the before posted code, i.e. with

type: entities
entities:
  - entity: input_text.fancy_meals_2
    card_mod:
      style:                
        ha-textfield $: |
          .mdc-text-field {
            height: 3em !important;
          }
          .mdc-text-field__input {
            align-self: center !important;
          }
          span#label {
            display: none !important;
          }

For me this is what an input_text should look like by default and I have > 10 of them. So I would rather have it in my theme.
But I seem to be getting either the syntax wrong or it needs to be placed differently when added to a theme rather than the card.

EDIT: The --text-field-padding can also be achieved by card-mod-card: | .card-content { padding: 0em 0px 0px 0px; }
That is part of my default theme.
It is really about accessing the mdc-text-field.

I guess these styles for input_text were taken from here:
card-mod thread ā†’ 1st post ā†’ link at the bottom ā†’ styles for input_text (+ how to make ā€¦ look less different).
So what is the problem with adding this to a theme?

Just checked - yes, same, a difference is only in a height: 40px vs 3em

EDIT: Wait, I need to check that I did not use your card-mod for the card and overlooked your mod for the theme. One second please.

There was no mod for theme posted by me.
But a mod may be made for a theme by copy/pasting published styles to a proper place of a theme file.

Okay, yes, just wanted to double check that I did not miss you also posting a theme version.

So, my card-mod in the card is based on your post. Correct.

But in my theme I am not using yaml but the non-yaml. And there it does not seem to be working if I convert.

I thought this would be the way to add it to a theme:

card-mod-row: |
        ha-textfield .mdc-text-field {
          height: 3em;
        }
        ha-textfield .mdc-text-field__input {
          align-self: center;
        }
        ha-textfield span#label {
          display: none;
        }

But it is not. Neither if I use card-mod-card instead of row.

So I do not know what the problem is other than it not oing anything when I add it to my theme.
Hence I think my conversion from card-mod ā€œyaml in a cardā€ to the non-yaml in my theme seems wrong.

Would it make life easier to use card-mod-<thing>-yaml? Is that then direct copy&paste from the card?
Because to be honest, I could not find anywhere why there is a yaml and non-yaml version in theming. Bu it seems that originally most examples were the non-yaml.