|
| 1 | +package com.huanshankeji.compose.web |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import com.huanshankeji.compose.web.attributes.attrs |
| 5 | +import com.huanshankeji.compose.web.attributes.plus |
| 6 | +import com.huanshankeji.compose.web.css.FIT_CONTENT |
| 7 | +import com.huanshankeji.compose.web.css.Styles |
| 8 | +import com.huanshankeji.compose.web.css.width |
| 9 | +import com.huanshankeji.compose.web.css.wrapInAttrs |
| 10 | +import org.jetbrains.compose.web.css.* |
| 11 | +import org.jetbrains.compose.web.dom.AttrBuilderContext |
| 12 | +import org.jetbrains.compose.web.dom.ContentBuilder |
| 13 | +import org.jetbrains.compose.web.dom.Div |
| 14 | +import org.w3c.dom.HTMLDivElement |
| 15 | + |
| 16 | +// try to follow names in Android Jetpack Compose |
| 17 | + |
| 18 | +@Composable |
| 19 | +fun Flexbox( |
| 20 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 21 | + content: ContentBuilder<HTMLDivElement> |
| 22 | +) = |
| 23 | + Div(attrs<HTMLDivElement> { |
| 24 | + style { |
| 25 | + display(DisplayStyle.Flex) |
| 26 | + } |
| 27 | + } + attrs, content) |
| 28 | + |
| 29 | +@Composable |
| 30 | +fun Flexbox(styles: Styles? = null, content: ContentBuilder<HTMLDivElement>) = |
| 31 | + Flexbox(styles.wrapInAttrs(), content) |
| 32 | + |
| 33 | +@Composable |
| 34 | +fun Column( |
| 35 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 36 | + fitContent: Boolean = true, |
| 37 | + content: ContentBuilder<HTMLDivElement> |
| 38 | +) = |
| 39 | + Flexbox(attrs<HTMLDivElement> { |
| 40 | + style { |
| 41 | + flexDirection(FlexDirection.Column) |
| 42 | + if (fitContent) width(FIT_CONTENT) |
| 43 | + } |
| 44 | + } + attrs, content) |
| 45 | + |
| 46 | +@Composable |
| 47 | +fun Column(styles: Styles? = null, fitContent: Boolean = true, content: ContentBuilder<HTMLDivElement>) = |
| 48 | + Column(styles.wrapInAttrs(), fitContent, content) |
| 49 | + |
| 50 | +@Composable |
| 51 | +fun ColumnWithSpaceBetween( |
| 52 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 53 | + fitContent: Boolean = true, |
| 54 | + content: ContentBuilder<HTMLDivElement> |
| 55 | +) = |
| 56 | + Column(attrs<HTMLDivElement> { |
| 57 | + style { |
| 58 | + justifyContent(JustifyContent.SpaceBetween) |
| 59 | + } |
| 60 | + } + attrs, fitContent, content) |
| 61 | + |
| 62 | + |
| 63 | +@Composable |
| 64 | +fun Row( |
| 65 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 66 | + content: ContentBuilder<HTMLDivElement> |
| 67 | +) = |
| 68 | + Flexbox(attrs<HTMLDivElement> { |
| 69 | + style { |
| 70 | + flexDirection(FlexDirection.Row) |
| 71 | + } |
| 72 | + } + attrs, content) |
| 73 | + |
| 74 | +@Composable |
| 75 | +fun Row( |
| 76 | + styles: Styles? = null, |
| 77 | + content: ContentBuilder<HTMLDivElement> |
| 78 | +) = |
| 79 | + Row(styles.wrapInAttrs(), content) |
| 80 | + |
| 81 | +@Composable |
| 82 | +fun RowWithSpaceBetween( |
| 83 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 84 | + content: ContentBuilder<HTMLDivElement> |
| 85 | +) = |
| 86 | + Row(attrs<HTMLDivElement> { |
| 87 | + style { |
| 88 | + justifyContent(JustifyContent.SpaceBetween) |
| 89 | + } |
| 90 | + } + attrs, content) |
| 91 | + |
| 92 | +@Composable |
| 93 | +fun ColumnWithGaps( |
| 94 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 95 | + gap: CSSNumeric, |
| 96 | + fitContent: Boolean = true, |
| 97 | + content: ContentBuilder<HTMLDivElement> |
| 98 | +) = |
| 99 | + Column(attrs<HTMLDivElement> { |
| 100 | + style { |
| 101 | + gap(gap) |
| 102 | + } |
| 103 | + } + attrs, fitContent, content) |
| 104 | + |
| 105 | +@Composable |
| 106 | +fun RowWithGaps( |
| 107 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 108 | + gap: CSSNumeric, |
| 109 | + content: ContentBuilder<HTMLDivElement> |
| 110 | +) = |
| 111 | + Row(attrs<HTMLDivElement> { |
| 112 | + style { |
| 113 | + gap(gap) |
| 114 | + } |
| 115 | + } + attrs, content) |
| 116 | + |
| 117 | +@Composable |
| 118 | +fun Centered( |
| 119 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 120 | + content: ContentBuilder<HTMLDivElement> |
| 121 | +) = |
| 122 | + Flexbox(attrs<HTMLDivElement> { |
| 123 | + style { |
| 124 | + alignItems(AlignItems.Center) |
| 125 | + justifyContent(JustifyContent.Center) |
| 126 | + } |
| 127 | + } + attrs, content) |
| 128 | + |
| 129 | +@Composable |
| 130 | +fun CenteredInViewport( |
| 131 | + attrs: AttrBuilderContext<HTMLDivElement>? = null, |
| 132 | + content: ContentBuilder<HTMLDivElement> |
| 133 | +) = |
| 134 | + Centered(attrs<HTMLDivElement> { |
| 135 | + style { |
| 136 | + minHeight(100.vh) |
| 137 | + } |
| 138 | + } + attrs, content) |
| 139 | + |
| 140 | +@Composable |
| 141 | +fun FrGrid( |
| 142 | + numColumns: Int, |
| 143 | + gap: CSSNumeric, |
| 144 | + content: HTMLElementContent |
| 145 | +) = |
| 146 | + Div({ |
| 147 | + style { |
| 148 | + display(DisplayStyle.Grid) |
| 149 | + gridTemplateColumns("repeat($numColumns, 1fr)") |
| 150 | + gap(gap) |
| 151 | + } |
| 152 | + }, content) |
| 153 | + |
| 154 | +@Composable |
| 155 | +fun Spacer(numPxs: Int) = |
| 156 | + TODO() as Unit |
0 commit comments