noredink-ui/styleguide-app/Examples/SegmentedControl.elm

101 lines
1.6 KiB
Elm
Raw Normal View History

module Examples.SegmentedControl
exposing
( Msg
, State
, example
, init
, styles
, update
)
2018-03-24 05:05:34 +03:00
{-|
@docs Msg
@docs State
@docs example
@docs init
@docs styles
@docs update
2018-03-24 05:05:34 +03:00
-}
import Css
import Css.Foreign
2018-03-24 05:05:34 +03:00
import Html
import ModuleExample exposing (Category(..), ModuleExample)
Style the segmented control tabs directly tl;dr; Use a class for each variant instead of overriding one variant. Before, we relied on CSS specificity in an unclear way. The `Focused` class was applying properly because it was ordered later than the `Tab` class in the stylesheet. The ordering that is important is the ordering in `styles` value. Since `elm-css` generates the stylesheet in the order of the lists, the `Focused` rule would be generated after the `Tab` rule. Meaning the `Focused` rule would take precedence over the `Tab` rule if an element had both classes as it was defined later in the stylesheet. There are some concerns with this approach: 1. It's not readily apparent that the ordering in `styles` is important. It is pretty easy to change the ordering of the list and have it break the styling. 2. We rely on `elm-css` to generate the stylesheet in a specific order. If it changes the order of rules it generates, we're almost surely going to break the styling. 3. Altering styles for tabs that are not focused is even less intuitive. Since the specificity is the same, you might not know why a given rule applies (or doesn't apply). Rather, we can eschew the specificity/precedence issues by applying a different class to each tab. The stuff that is the same can stay on the `Tab` class, and the stuff that differs can be on different classes. We are now able to set the background color for `Unfocused` tabs. We were relying on the control being placed atop a white background. When we moved to using the control atop a non-whitebackground, it showed that the `Unfocused` tabs had a transparent backround. All of our designs show `Unfocused` tabs with a white backround. See https://github.com/NoRedInk/noredink-ui/pull/14 for more information.
2018-03-30 18:51:29 +03:00
import Nri.Ui.SegmentedControl.V5
import Nri.Ui.Styles.V1
2018-03-24 05:05:34 +03:00
{-| -}
type Msg
= Select Id
{-| -}
type alias State =
Style the segmented control tabs directly tl;dr; Use a class for each variant instead of overriding one variant. Before, we relied on CSS specificity in an unclear way. The `Focused` class was applying properly because it was ordered later than the `Tab` class in the stylesheet. The ordering that is important is the ordering in `styles` value. Since `elm-css` generates the stylesheet in the order of the lists, the `Focused` rule would be generated after the `Tab` rule. Meaning the `Focused` rule would take precedence over the `Tab` rule if an element had both classes as it was defined later in the stylesheet. There are some concerns with this approach: 1. It's not readily apparent that the ordering in `styles` is important. It is pretty easy to change the ordering of the list and have it break the styling. 2. We rely on `elm-css` to generate the stylesheet in a specific order. If it changes the order of rules it generates, we're almost surely going to break the styling. 3. Altering styles for tabs that are not focused is even less intuitive. Since the specificity is the same, you might not know why a given rule applies (or doesn't apply). Rather, we can eschew the specificity/precedence issues by applying a different class to each tab. The stuff that is the same can stay on the `Tab` class, and the stuff that differs can be on different classes. We are now able to set the background color for `Unfocused` tabs. We were relying on the control being placed atop a white background. When we moved to using the control atop a non-whitebackground, it showed that the `Unfocused` tabs had a transparent backround. All of our designs show `Unfocused` tabs with a white backround. See https://github.com/NoRedInk/noredink-ui/pull/14 for more information.
2018-03-30 18:51:29 +03:00
Nri.Ui.SegmentedControl.V5.Config Id Msg
2018-03-24 05:05:34 +03:00
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
Style the segmented control tabs directly tl;dr; Use a class for each variant instead of overriding one variant. Before, we relied on CSS specificity in an unclear way. The `Focused` class was applying properly because it was ordered later than the `Tab` class in the stylesheet. The ordering that is important is the ordering in `styles` value. Since `elm-css` generates the stylesheet in the order of the lists, the `Focused` rule would be generated after the `Tab` rule. Meaning the `Focused` rule would take precedence over the `Tab` rule if an element had both classes as it was defined later in the stylesheet. There are some concerns with this approach: 1. It's not readily apparent that the ordering in `styles` is important. It is pretty easy to change the ordering of the list and have it break the styling. 2. We rely on `elm-css` to generate the stylesheet in a specific order. If it changes the order of rules it generates, we're almost surely going to break the styling. 3. Altering styles for tabs that are not focused is even less intuitive. Since the specificity is the same, you might not know why a given rule applies (or doesn't apply). Rather, we can eschew the specificity/precedence issues by applying a different class to each tab. The stuff that is the same can stay on the `Tab` class, and the stuff that differs can be on different classes. We are now able to set the background color for `Unfocused` tabs. We were relying on the control being placed atop a white background. When we moved to using the control atop a non-whitebackground, it showed that the `Unfocused` tabs had a transparent backround. All of our designs show `Unfocused` tabs with a white backround. See https://github.com/NoRedInk/noredink-ui/pull/14 for more information.
2018-03-30 18:51:29 +03:00
{ filename = "Nri/Ui/SegmentedControl/V5.elm"
2018-03-24 05:05:34 +03:00
, category = Behaviors
, content =
[ Html.map parentMessage
(styles.div Container [ Nri.Ui.SegmentedControl.V5.view state ])
2018-03-24 05:05:34 +03:00
]
}
{-| -}
init : State
init =
{ onClick = Select
, options =
[ { icon = Nothing
, id = "a"
, label = "Option A"
, value = "a"
}
, { icon = Nothing
, id = "b"
, label = "Option B"
, value = "b"
}
2018-03-24 05:05:34 +03:00
]
, selected = "a"
}
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
Select id ->
( { state | selected = id }, Cmd.none )
{-| -}
styles : Nri.Ui.Styles.V1.Styles a b c
styles =
Nri.Ui.Styles.V1.styles
"Examples-SegmentedControl-"
[ Css.Foreign.class Container
[ Css.width (Css.px 500)
]
]
type Classes
= Container
2018-03-24 05:05:34 +03:00
-- INTERNAL
type alias Id =
String