Last active
June 19, 2023 19:26
-
-
Save opsb/6c521c7644b5d051b61f7eec0a038ba2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module View.Grid exposing (view) | |
import Element exposing (Element, column, el, fill, row, spacing, width) | |
import List.Extra | |
type alias Config = | |
{ itemsPerRow : Int | |
, horizontalSpacing : Int | |
, verticalSpacing : Int | |
} | |
view : Config -> List (Element msg) -> Element msg | |
view config items = | |
let | |
groups = | |
items | |
|> padMissing config | |
|> List.map cellView | |
|> List.Extra.groupsOf config.itemsPerRow | |
in | |
column [ width fill, spacing config.verticalSpacing ] <| | |
List.map (rowView config) groups | |
rowView : Config -> List (Element msg) -> Element msg | |
rowView config items = | |
row [ width fill, spacing config.horizontalSpacing ] items | |
padMissing : Config -> List (Element msg) -> List (Element msg) | |
padMissing config items = | |
let | |
remainder = | |
remainderBy config.itemsPerRow (List.length items) | |
in | |
items ++ List.repeat remainder Element.none | |
cellView : Element msg -> Element msg | |
cellView body = | |
el [ width fill ] body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Grid.view | |
{ rowCount = 3 | |
, horizontalSpacing = 10 | |
, verticalSpacing = 10 | |
} | |
[ el [] (text "cell one") | |
, el [] (text "cell two") | |
, el [] (text "cell three") | |
, el [] (text "cell four") | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment