• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import m from 'mithril';
16
17// Check if a mithril component vnode has children
18export function hasChildren<T>({children}: m.Vnode<T>): boolean {
19  return (
20    Array.isArray(children) &&
21    children.length > 0 &&
22    children.some((value) => value)
23  );
24}
25
26// A component which simply passes through it's children.
27// Can be used for having something to attach lifecycle hooks to without having
28// to add an extra HTML element to the DOM.
29export const Passthrough = {
30  view({children}: m.VnodeDOM) {
31    return children;
32  },
33};
34
35export interface GateAttrs {
36  open: boolean;
37}
38
39// The gate component is a wrapper which can either be open or closed.
40// - When open, children are rendered inside a div where display = contents.
41// - When closed, children are rendered inside a div where display = none
42// Use this component when we want to conditionally render certain children,
43// but we want to maintain their state.
44export const Gate = {
45  view({attrs, children}: m.VnodeDOM<GateAttrs>) {
46    return m(
47      '',
48      {
49        style: {display: attrs.open ? 'contents' : 'none'},
50      },
51      children,
52    );
53  },
54};
55