Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. module Colorpicker exposing (main)
  2.  
  3. -- The Browser module has the definition we need to run an Elm application in the
  4. -- Elm architecture.
  5. -- Html is the module that defines the functions `div`, `span`, `button`, etc.
  6. -- Html.Attributes exposes functions for element attributes such as `class` and `id`.
  7. -- Html.Events exposes functions for binding elements such as `onClick` and `onInput`.
  8.  
  9. import Browser
  10. import Html exposing (..)
  11. import Html.Attributes exposing (..)
  12. import Html.Events exposing (..)
  13.  
  14.  
  15. {-| We call `Browser.element` with a record passing our definitions of the functions.
  16. The Elm runtime is responsible for calling each of those functions in the
  17. appropriate moment.
  18. -}
  19. main =
  20. Browser.element
  21. { init = init
  22. , view = view
  23. , update = update
  24. , subscriptions = subscriptions
  25. }
  26.  
  27.  
  28. {-| Subscriptions are used to listen for external events, such as websockets
  29. and mouse drag events. We're not using in this component, so we can just
  30. return `Sub.none`.
  31. -}
  32. subscriptions model =
  33. Sub.none
  34.  
  35.  
  36. {-| Constant of possible colors users can choose from.
  37. -}
  38. colors : List String
  39. colors =
  40. [ "#4286f4", "#41f441", "#dfe212", "#e25712", "#e21212" ]
  41.  
  42.  
  43. {-| Our model definition, described earlier.
  44. -}
  45. type alias Model =
  46. { selectedColor : String
  47. }
  48.  
  49.  
  50. {-| Initialization argument passed in `Elm.Colorpicker.init`.
  51. -}
  52. type alias Flags =
  53. { color : String
  54. }
  55.  
  56.  
  57. {-| The init function is called on load. We're not receiving any arguments (called flags)
  58. right now, but we'll need them in a second.
  59. -}
  60. init : Flags -> ( Model, Cmd Msg )
  61. init flags =
  62. ( { selectedColor = flags.color }, Cmd.none )
  63.  
  64.  
  65. {-| List of actions users can perform in this component
  66. -}
  67. type Msg
  68. = ColorSelected String
  69. | SelectionCleared
  70.  
  71.  
  72. {-| The `update` function changes the model based on the received `Msg`. To be strict about
  73. terms, nothing is being mutated, we're just returning a changed version of the model.
  74. -}
  75. update : Msg -> Model -> ( Model, Cmd Msg )
  76. update msg model =
  77. case msg of
  78. ColorSelected hash ->
  79. ( { model | selectedColor = hash }, Cmd.none )
  80.  
  81. SelectionCleared ->
  82. ( { model | selectedColor = "" }, Cmd.none )
  83.  
  84.  
  85. {-| The `view` function receives the model and produces HTML. The Elm runtime is responsible
  86. for all dom diffing and optimizations.
  87. -}
  88. view : Model -> Html Msg
  89. view model =
  90. div [ id "colorpicker" ]
  91. [ span []
  92. [ label [ class "font-bold" ] [ text "Choose a color" ]
  93. , viewClearSelection model
  94. ]
  95. , div [ class "flex items-center" ] (List.map (\hex -> viewColor (model.selectedColor == hex) hex) colors)
  96. , input [ type_ "hidden", name "color", value model.selectedColor ] []
  97. ]
  98.  
  99.  
  100. viewClearSelection : Model -> Html Msg
  101. viewClearSelection { selectedColor } =
  102. if selectedColor == "" then
  103. text ""
  104.  
  105. else
  106. span [ onClick SelectionCleared, class "text-sm ml-1 italic cursor-pointer" ] [ text "— Clear selection" ]
  107.  
  108.  
  109. viewColor : Bool -> String -> Html Msg
  110. viewColor isSelected hex =
  111. let
  112. className =
  113. if isSelected then
  114. "w-8 h-8 m-1 rounded shadow"
  115.  
  116. else
  117. "w-6 h-6 m-1 rounded"
  118. in
  119. div
  120. [ onClick (ColorSelected hex)
  121. , class className
  122. , style "background" hex
  123. ]
  124. []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement