OH 5: MainUI Widgets Best Practices

I’ll do my best, but no promises.

I think part of what’s driving the issue with the docs and general information about the widget system how expansive it has become. @ysc gave us an amazing base system with his work building the MainUI for OH3, but it has blossomed into so much more since then. And now what a user feels like should be a simple question very rapidly runs into a gray area where the discussion moves from OH specific details and ideas briefly through the underlying f7 library and then before you know it you’re into full on web dev.

That said, despite having somehow landed in the position of one of the OH MainUI experts (I’m still not sure how this happened), I’m no web dev. I teach at a medical school. I’ve been hobbying computers since you had to put the OS 5.25" disc into drive A and the Application 5.25" into drive B (what’s a C: drive?). I’ve been scripting since the Basic compiler on my old TI-99. But, I have exactly 0 minutes formal education in any of this. So, I make no claim at all that my recommendations are best practices; but these are my practices and the things I’ve learned from trial and error and many hours of feeding my curiosity and urge to tinker.

This is actually a good question to start with. My answer: I don’t.

I never attempt widget creation without the browser dev tools elements tab open and make extensive use of the inspection function. Going beyond the very basic steps of widget creation it really is important to see past the components to the html:

When you do this, it becomes clear that blocks rows and cols are just the most basic of div elements each with a single f7 class added to them:

Widget yamlResultant html
component: f7-block
config:
  id: block1
slots:
  default:
    - component: f7-row
      config:
        id: row1
      slots:
        default:
          - component: f7-col
            config:
              id: col1
<div class="block" id="block1">
  <div class="row" slot="default" id="row1">
    <div class="col" slot="default" id="col1">
    </div>
  </div>
</div>

It’s those classes, of course, where the the problems occur. F7 does a lot under the hood. Here’s a list of the f7-block specific css variables, and that’s just the ones they expect you to want to change. All this extra work done for you is what makes it such a good tool, but also where most of the “black-box” nature of the widget system comes from. F7 uses those base component classes not just to apply css styles, but to identify the elements targeted by each of it’s many scripts.

So, when do I use blocks and rows and cols? Only when that component is about matching the style and spacing of the UI. If it’s about having better fine control of the child elements, then I’ll often just use a div instead because I don’t want all that other stuff.

Here’s an example. I have a widget I’ve created to match the style of MainUI configuration detail pages so I can display some of my more complex personal systems in a consistent way:

This widget extensively uses blocks and rows because those are how the MainUI page itself is built. But my timed confirm button uses only div elements for its structure.

The warning about performance with the overuse of blocks, rows, and cols, also comes from that F7 under-the-hood stuff and the many scripts it runs to modify nearly every F7 component, but to be honest I have never seen much of an issue except maybe the tiniest of slowdowns on the very oldest of devices.

Again, I don’t always. On the other hand, that root component is where a lot of the base css (either in a style object or in a stylesheet) is going to go so it does often make sense to have some basic container (either a block or a div) as the root. An f7-card and all of its oh derivatives also start with just a div but it’s a div with even more styling and scripting than a block so that’s why cards also make good root components.

The block feels to me like it comes with the least baggage, so it’s a good default choice , but there are plenty of reasons why you may want to use one of the others. If you know that from the start your widget is going to have a vertical layout, maybe the col is a better root component.

There are very few places where MainUI puts any restrictions on what you can and can’t do. There are a handful of examples, e.g., an oh-grid-col in the block section of a layout page will not render more than the first child component, but they are few and far between. The system could, probably, have more safeguards in that respect but html, in general is so flexible and robust, that it’s pretty difficult to break things, you just get widget that don’t look like you where expecting and don’t do what you wan them to.

Finally, an easy one!

Those classes all come from F7 and are listed in the docs page intuitively titled ‘Typography’:

This is, however, an excellent example of what I said at the start. This should be a fairly straightforward question for a user just starting out with widgets, but…

All these classes do is give you a kinda-maybe-sorta shortcut to applying the css variable of that name. So, the answer to whether a class impacts the component or the component and all its children depends on which css property you are dealing with. So now, you really need to starting thinking about css inheritance rules and which properties are inherited and which are non-inherited. The good news is, if I recall, most of the properties that are tied to these F7 classes are non-inherited so if you apply the class to a component, it will only impact that component.

I never use these classes myself. Because the OH widget syntax gives you such easy access to most basic css through the style object there’s simply no benefit to doing this through the slightly more opaque F7 classes.

Well, so much for easy questions…

I would bet that there are more pages on the internet about this one topic than there are threads in this entire forum. Here we’ve gone fully past any grey area and are simply talking straight up web dev. This sort of sizing will also be the most difficult part of just about any complex widget. I can make the most meager start at barely scratching the surface.

In an ideal world, this isn’t something you need to worry about. Most html elements, by default, will have a minimum size to encompass all their child elements (the default value for both width and height css properties is auto. No web page, however, is an ideal world, because the minute you are talking about a discrete page there are already constraints.

My first piece of advice in this sort of situation is always do as little as possible; get out of the way, just let html and css do their jobs, and see if that works. This will of course only get you so far because, you guessed it, all those F7 components are packing extra settings that are going to get in way. That’s when you then need to start thinking about applying you’re own width and height values. Which part (or more likely parts) are going to need those values may be difficult to determine. This is another situation where browser dev tools are very helpful. The css pane in the elements tab has a computed section which will give you a kind of visual outline of what the final css values are for each property and you can sometimes see what’s causing something to only be X pixels tall when you thought it should be Y. Or it will show you that there are no height or width values for this element and so it is just passively getting those from its children (what you presumably want in a case like this).

There are probably people out here who can look at a whole section of html and identify on the first try where a css size or position bottleneck is, but I’m not one of them. It usually takes me a couple of tries to find the right combination of tweaks. For all know that sort of trial and error is css best practice, but I doubt it.

Well from the previous answer, this just applies the flex value to the container’s display property. This means it turns the container into a flexbox. Flexbox and grid are two of the best ways to get responsive sizing and spacing in css, and will always be my two first choices when building a widget (although again, instead of using the display-flex class I’ll just add display: flex to the components style object.

In fact, you’re already using it quite a bit. One of the default css settings for the f7-row is display: flex;. It’s part of the answer to your specific questions below about you widget. One of the links I’ve probably posted the most over the years (and I know Yannick has posted several times as well) is a good basic introduction in what flexbox can do and how to use it.

Also on the F7 site, on an actually sensibly named page, you can find a list of all the default values for the f7 css variables: CSS variables. This doesn’t include the css directives that are set without variables (i.e., the things they don’t want you to be able to easily override). To my knowledge, those values you will just have to find using the dev tools.

In my opinion, almost never. I think I’ve even gone back at this point and changed any of my early widgets that did use absolute positions to relative positions. Even in situations where you are working around absolute things like bars or panels on a page, you are better off calculating a relative value from that absolute value (e.g., calc(100% - 32px)).

So applying all this to your example questions about the widget. Here’s my process:

The first thing I’m going to do is bust out the dev tools and see if I can identify that space:

That tells us it’s the top margin of the block element that is inside the card contents. So, first we check if we can override that the easy way:

- component: f7-block
  config:
    style:
      margin-top: 0

Success!

These are part of the same answer. Here you’ve over-used the f7-col and and f7 classes (and added some absolute positioning as well):

You want the children within this f7-row to stretch fully from end to end. With flexbox (which the f7-row is by default) the justify-content: center; css directive tells the flexbox to try and squeeze all the child elements as close to the center as possible. The only reason you don’t see them all adjacent is that you have put them in cols which adds a bunch of automatic size and spacing.

Following the philosophy of getting out of the way and letting the flexbox do the work, I recommend the following:

  1. Get rid of both the classes on the row: space-between is the default justification setting for the row and that’s what you want here and start is already the item align for the row as well.
  2. Get rid of all 3 cols: you have a precise idea where you want these child elements (fully left, center, and fully right) and you don’t want the cols making extra decisions on size and spacing that will interfere with that.
  3. Get rid of the top, right, and position style settings on the chip: any child elements with absolute positioning will ignore the position directives from the flexbox parent and you want the parent to set the position here.

Those changes result in:

This is just a basic web dev structure thing. Again, you’ve over engineered the row and columns when you just really need one container every time you want two or more things to share some organization:

  1. you want the power label and it’s toggle to remain side by side: sounds like a row → f7-row1
  2. you want the display label and it’s toggle to remain side by side: sounds like a row → f7-row2
  3. you want f7-row1 and f7-row2 to be stacked vertically: sounds like a column to me → f7-col1
  4. you want the water and mode labels to be stacked vertically: another column! → f7-col2
  5. you want f7-col1 and f7-col2 to remain side by side: another row → f7-row3
  6. you want knob1 and its label stacked vertically: column → f7-col3
  7. you want knob2 and its label stacked vertically: column → f7-col4
  8. you want f7-col3 and f7-col4 to remain side by side: another row → f7-row4
  9. you want f7-row3 and f7-row4 to be spaced automatically across the width of the card: flexbox time! → f7-row5

So that looks like this structure-wise:

f7-row (#5)
  f7-row (#4)
    f7-col (#4)
      knob
      label
    f7-col (#3)
      knob
      label
  f7-row (#3)
    f7-col (#2)
      label
      label
    f7-col (#1)
      f7-row (#2)
        label
        toggle
      f7-row (#1)
        label
        toggle

Here’s the result of that structure with absolutely no additional classes or setting on any of those rows or columns:

Pretty close! Looks like we just have a little bit of tweaking to do. I see two issues to start with:

  1. we want the knob labels centered: that’s easy to fix. The labels are already centered in the column (you can check dev tools to verify this) but the text isn’t centered in the labels, so we just add text-align: center to the label styles:
  2. The rows that hold the toggles and their labels shrank so much that flex box decided it was easier to split into two rows: also an easy fix. We can use flex-wrap: nowrap to tell those rows to always be only one row.

Almost there! The toggle + labels are a little off. We;d like some space between them and we don’t want the lower one to push beyond the edge of the card.

  1. The space issue is easy. Flexbox comes with a gap property that gives a consistent space between all children. So in this case well add gap: 5px to the styles of the rows that hold the toggles and their labels.
  2. the position of the lower row is a little trickier. Flexbox has made a poor choice about how to space and size it’s child columns. The basic answer is that we need the felx box to preserve a little more space for that last column. There are enough different flexboxes (f7-rows) in this structure that we have a couple of choices, but I think the most direct one is to tell the columns on the right side to be slightly different sizes. In this case we want the column holding all the toggle pieces to expand more than the column holding the simple labels. flex-grow is the css to determine what proportion of the available space a flex child item takes up (with the default being 1). So if we set that last column to flex-grow: 1.5 it’ll be a little larger and the toggles will fit.

Looks great! And is fully responsive to size:

Of course there are still many things we can continue to tweak until it’s perfect. Maybe you want more space between the water/mode labels and the toggles. Identify the correct row container and add a gap style. Maybe you want more space between the top row and the bottom row on the right side. gap works for flex columns too (just make sure you add it to both those columns)…etc.

*This is not the only structure that solves this arrangement. The right side could also be just one column with two rows and each row has two sub rows which hold a label and a label + toggle respectively. Or you could go with a css grid…or…etc.

I suspect, that once you’ve gone through and trimmed down the extra cols and rows and such you’ll find you don’t need the hard-coded height anymore. If I’m wrong, post a follow up and I can work through figuring that out too.

One last note: This is may be a slightly awkward time for this answer. Several people have done an amazing job in the last few months updating MainUI from Vue2 to Vue3 and, in fact, the newest snapshots at this point are on Vue3. This change should be fairly invisible for most users and will only impact the stuff here a little bit, but as I understand it, it is also a prelude to at some point moving OH to F7 version 8 from F7 version 7 and that could impact quite a bit (for example f7-rows and f7-cols don’t exist in version 8).