2019-06-10 23:58:12 +03:00
|
|
|
module Examples.Modal exposing (Msg, State, example, init, update, subscriptions)
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
|
2019-06-10 23:58:12 +03:00
|
|
|
@docs Msg, State, example, init, update, subscriptions
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2019-06-11 00:28:38 +03:00
|
|
|
import Accessibility.Styled as Html exposing (Html, div, h3, h4, p, text)
|
2018-08-29 22:09:22 +03:00
|
|
|
import Css exposing (..)
|
2019-06-11 04:24:09 +03:00
|
|
|
import Css.Global
|
2019-06-12 21:25:10 +03:00
|
|
|
import Html as Root
|
2018-08-29 22:09:22 +03:00
|
|
|
import Html.Styled.Attributes exposing (css)
|
|
|
|
import ModuleExample exposing (Category(..), ModuleExample)
|
2019-07-26 08:14:26 +03:00
|
|
|
import Nri.Ui.Button.V9 as Button
|
2019-06-11 00:28:38 +03:00
|
|
|
import Nri.Ui.Checkbox.V5 as Checkbox
|
2018-08-29 22:09:22 +03:00
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
2019-08-16 20:06:20 +03:00
|
|
|
import Nri.Ui.Modal.V7 as Modal
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2019-06-10 23:58:12 +03:00
|
|
|
{ infoModal : Modal.Model
|
|
|
|
, warningModal : Modal.Model
|
2019-06-11 00:28:38 +03:00
|
|
|
, visibleTitle : Bool
|
2019-06-11 00:53:38 +03:00
|
|
|
, showX : Bool
|
2019-06-11 03:12:35 +03:00
|
|
|
, showContinue : Bool
|
2019-06-11 04:32:54 +03:00
|
|
|
, showSecondary : Bool
|
2019-06-11 21:26:24 +03:00
|
|
|
, dismissOnEscAndOverlayClick : Bool
|
2019-06-11 00:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
2019-06-12 21:12:37 +03:00
|
|
|
{ infoModal = Modal.init
|
|
|
|
, warningModal = Modal.init
|
2019-06-11 00:28:38 +03:00
|
|
|
, visibleTitle = True
|
2019-06-11 03:33:53 +03:00
|
|
|
, showX = True
|
|
|
|
, showContinue = True
|
2019-06-11 04:32:54 +03:00
|
|
|
, showSecondary = False
|
2019-06-11 21:26:24 +03:00
|
|
|
, dismissOnEscAndOverlayClick = True
|
2019-06-10 23:58:12 +03:00
|
|
|
}
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
example : (Msg -> msg) -> State -> ModuleExample msg
|
|
|
|
example parentMessage state =
|
2019-08-16 20:06:20 +03:00
|
|
|
{ name = "Nri.Ui.Modal.V7"
|
2018-08-29 22:09:22 +03:00
|
|
|
, category = Modals
|
|
|
|
, content =
|
2019-07-26 08:14:26 +03:00
|
|
|
[ Button.button "Launch Info Modal"
|
|
|
|
[ Button.onClick (InfoModalMsg (Modal.open "launch-info-modal"))
|
|
|
|
, Button.custom
|
|
|
|
[ Html.Styled.Attributes.id "launch-info-modal"
|
|
|
|
, css [ Css.marginRight (Css.px 16) ]
|
|
|
|
]
|
|
|
|
, Button.secondary
|
|
|
|
, Button.medium
|
|
|
|
]
|
|
|
|
, Button.button "Launch Warning Modal"
|
|
|
|
[ Button.onClick (WarningModalMsg (Modal.open "launch-warning-modal"))
|
|
|
|
, Button.custom [ Html.Styled.Attributes.id "launch-warning-modal" ]
|
|
|
|
, Button.secondary
|
|
|
|
, Button.medium
|
|
|
|
]
|
2019-06-11 20:23:13 +03:00
|
|
|
, Modal.info
|
2019-07-26 08:14:26 +03:00
|
|
|
{ title = "Modal.info"
|
|
|
|
, visibleTitle = state.visibleTitle
|
2019-06-11 20:23:13 +03:00
|
|
|
, wrapMsg = InfoModalMsg
|
2019-06-18 19:00:01 +03:00
|
|
|
, content =
|
|
|
|
viewContent state
|
|
|
|
InfoModalMsg
|
2019-07-26 08:14:26 +03:00
|
|
|
Button.primary
|
|
|
|
Button.secondary
|
2019-06-10 23:58:12 +03:00
|
|
|
}
|
|
|
|
state.infoModal
|
|
|
|
, Modal.warning
|
2019-07-26 08:14:26 +03:00
|
|
|
{ title = "Modal.warning"
|
|
|
|
, visibleTitle = state.visibleTitle
|
2019-06-11 20:23:13 +03:00
|
|
|
, wrapMsg = WarningModalMsg
|
2019-06-18 19:00:01 +03:00
|
|
|
, content =
|
|
|
|
viewContent state
|
|
|
|
WarningModalMsg
|
2019-07-26 08:14:26 +03:00
|
|
|
Button.danger
|
|
|
|
Button.secondary
|
2019-06-10 23:58:12 +03:00
|
|
|
}
|
|
|
|
state.warningModal
|
2018-08-29 22:09:22 +03:00
|
|
|
]
|
|
|
|
|> List.map (Html.map parentMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-12 21:25:10 +03:00
|
|
|
viewContent :
|
|
|
|
State
|
|
|
|
-> (Modal.Msg -> Msg)
|
2019-07-26 08:14:26 +03:00
|
|
|
-> Button.Attribute Msg
|
|
|
|
-> Button.Attribute Msg
|
2019-06-18 18:54:20 +03:00
|
|
|
-> Modal.FocusableElementAttrs Msg
|
2019-06-12 21:25:10 +03:00
|
|
|
-> Html Msg
|
2019-07-26 08:14:26 +03:00
|
|
|
viewContent state wrapMsg firstButtonStyle secondButtonStyle focusableElementAttrs =
|
2019-07-26 08:26:19 +03:00
|
|
|
case ( state.showX, state.showContinue, state.showSecondary ) of
|
|
|
|
( True, True, True ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.closeButton wrapMsg focusableElementAttrs.firstFocusableElement
|
|
|
|
, Modal.viewContent [ viewSettings state ]
|
|
|
|
, Modal.viewFooter
|
|
|
|
[ Button.button "Continue"
|
|
|
|
[ firstButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
]
|
|
|
|
, Button.button "Close"
|
|
|
|
[ secondButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.lastFocusableElement
|
|
|
|
]
|
2019-07-26 08:14:26 +03:00
|
|
|
]
|
2019-07-26 08:26:19 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
( True, False, True ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.closeButton wrapMsg focusableElementAttrs.firstFocusableElement
|
|
|
|
, Modal.viewContent [ viewSettings state ]
|
|
|
|
, Modal.viewFooter
|
|
|
|
[ Button.button "Close"
|
|
|
|
[ secondButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.lastFocusableElement
|
|
|
|
]
|
2019-07-26 08:14:26 +03:00
|
|
|
]
|
2019-06-12 21:25:10 +03:00
|
|
|
]
|
|
|
|
|
2019-07-26 08:26:19 +03:00
|
|
|
( True, False, False ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.closeButton wrapMsg focusableElementAttrs.firstFocusableElement
|
|
|
|
, Modal.viewContent [ viewSettings state ]
|
|
|
|
]
|
|
|
|
|
|
|
|
( True, True, False ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.closeButton wrapMsg focusableElementAttrs.firstFocusableElement
|
|
|
|
, Modal.viewContent [ viewSettings state ]
|
|
|
|
, Modal.viewFooter
|
|
|
|
[ Button.button "Continue"
|
|
|
|
[ firstButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.lastFocusableElement
|
|
|
|
]
|
2019-07-26 08:14:26 +03:00
|
|
|
]
|
2019-06-12 21:25:10 +03:00
|
|
|
]
|
|
|
|
|
2019-07-26 08:26:19 +03:00
|
|
|
( False, True, True ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.viewContent [ viewSettings state ]
|
|
|
|
, Modal.viewFooter
|
|
|
|
[ Button.button "Continue"
|
|
|
|
[ firstButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.firstFocusableElement
|
|
|
|
]
|
|
|
|
, Button.button "Close"
|
|
|
|
[ secondButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.lastFocusableElement
|
|
|
|
]
|
2019-07-26 08:14:26 +03:00
|
|
|
]
|
2019-06-12 21:25:10 +03:00
|
|
|
]
|
2019-06-11 03:45:21 +03:00
|
|
|
|
2019-07-26 08:26:19 +03:00
|
|
|
( False, False, True ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.viewContent [ viewSettings state ]
|
|
|
|
, Modal.viewFooter
|
|
|
|
[ Button.button "Close"
|
|
|
|
[ secondButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.lastFocusableElement
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
( False, True, False ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.viewContent [ viewSettings state ]
|
|
|
|
, Modal.viewFooter
|
|
|
|
[ Button.button "Continue"
|
|
|
|
[ firstButtonStyle
|
|
|
|
, Button.onClick ForceClose
|
|
|
|
, Button.custom focusableElementAttrs.lastFocusableElement
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
( False, False, False ) ->
|
|
|
|
div []
|
|
|
|
[ Modal.viewContent [ viewSettings state ]
|
|
|
|
]
|
2019-06-11 03:52:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
viewSettings : State -> Html Msg
|
|
|
|
viewSettings state =
|
|
|
|
div []
|
|
|
|
[ Checkbox.viewWithLabel
|
|
|
|
{ identifier = "visible-title"
|
|
|
|
, label = "Visible title"
|
|
|
|
, selected = Checkbox.selectedFromBool state.visibleTitle
|
|
|
|
, setterMsg = SetVisibleTitle
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
|
|
|
{ identifier = "show-x"
|
|
|
|
, label = "Show X button"
|
|
|
|
, selected = Checkbox.selectedFromBool state.showX
|
|
|
|
, setterMsg = SetShowX
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
|
|
|
{ identifier = "show-continue"
|
|
|
|
, label = "Show main button"
|
|
|
|
, selected = Checkbox.selectedFromBool state.showContinue
|
|
|
|
, setterMsg = SetShowContinue
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
2019-06-11 04:32:54 +03:00
|
|
|
, Checkbox.viewWithLabel
|
|
|
|
{ identifier = "show-secondary"
|
|
|
|
, label = "Show secondary button"
|
|
|
|
, selected = Checkbox.selectedFromBool state.showSecondary
|
|
|
|
, setterMsg = SetShowSecondary
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
2019-06-12 21:12:37 +03:00
|
|
|
, Checkbox.viewWithLabel
|
|
|
|
{ identifier = "dismiss-on-click"
|
|
|
|
, label = "Dismiss on ESC and on backdrop click"
|
|
|
|
, selected = Checkbox.selectedFromBool state.dismissOnEscAndOverlayClick
|
|
|
|
, setterMsg = SetDismissOnEscAndOverlayClick
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
2019-06-11 03:52:19 +03:00
|
|
|
]
|
2019-06-11 03:45:21 +03:00
|
|
|
|
|
|
|
|
2018-08-29 22:09:22 +03:00
|
|
|
{-| -}
|
2019-06-11 00:28:38 +03:00
|
|
|
type Msg
|
|
|
|
= InfoModalMsg Modal.Msg
|
|
|
|
| WarningModalMsg Modal.Msg
|
2019-06-11 03:12:35 +03:00
|
|
|
| ForceClose
|
2019-06-11 00:28:38 +03:00
|
|
|
| SetVisibleTitle Bool
|
2019-06-11 00:53:38 +03:00
|
|
|
| SetShowX Bool
|
2019-06-11 03:12:35 +03:00
|
|
|
| SetShowContinue Bool
|
2019-06-11 04:32:54 +03:00
|
|
|
| SetShowSecondary Bool
|
2019-06-11 21:26:24 +03:00
|
|
|
| SetDismissOnEscAndOverlayClick Bool
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
|
|
|
update msg state =
|
2019-06-12 21:12:37 +03:00
|
|
|
let
|
|
|
|
updateConfig =
|
|
|
|
{ dismissOnEscAndOverlayClick = state.dismissOnEscAndOverlayClick }
|
|
|
|
in
|
2018-08-29 22:09:22 +03:00
|
|
|
case msg of
|
2019-06-10 23:58:12 +03:00
|
|
|
InfoModalMsg modalMsg ->
|
2019-06-12 21:12:37 +03:00
|
|
|
case Modal.update updateConfig modalMsg state.infoModal of
|
2019-06-10 23:58:12 +03:00
|
|
|
( newState, cmds ) ->
|
|
|
|
( { state | infoModal = newState }
|
|
|
|
, Cmd.map InfoModalMsg cmds
|
|
|
|
)
|
|
|
|
|
|
|
|
WarningModalMsg modalMsg ->
|
2019-06-12 21:12:37 +03:00
|
|
|
case Modal.update updateConfig modalMsg state.warningModal of
|
2019-06-10 23:58:12 +03:00
|
|
|
( newState, cmds ) ->
|
|
|
|
( { state | warningModal = newState }
|
|
|
|
, Cmd.map WarningModalMsg cmds
|
|
|
|
)
|
2018-08-29 22:09:22 +03:00
|
|
|
|
2019-06-11 03:12:35 +03:00
|
|
|
ForceClose ->
|
|
|
|
( { state
|
2019-06-12 21:12:37 +03:00
|
|
|
| infoModal = Modal.init
|
|
|
|
, warningModal = Modal.init
|
2019-06-11 03:12:35 +03:00
|
|
|
}
|
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
2019-06-11 00:28:38 +03:00
|
|
|
SetVisibleTitle value ->
|
|
|
|
( { state | visibleTitle = value }, Cmd.none )
|
|
|
|
|
2019-06-11 00:53:38 +03:00
|
|
|
SetShowX value ->
|
|
|
|
( { state | showX = value }, Cmd.none )
|
|
|
|
|
2019-06-11 03:12:35 +03:00
|
|
|
SetShowContinue value ->
|
|
|
|
( { state | showContinue = value }, Cmd.none )
|
|
|
|
|
2019-06-11 04:32:54 +03:00
|
|
|
SetShowSecondary value ->
|
|
|
|
( { state | showSecondary = value }, Cmd.none )
|
|
|
|
|
2019-06-11 21:26:24 +03:00
|
|
|
SetDismissOnEscAndOverlayClick value ->
|
2019-06-12 21:12:37 +03:00
|
|
|
( { state | dismissOnEscAndOverlayClick = value }, Cmd.none )
|
2019-06-11 21:26:24 +03:00
|
|
|
|
2018-08-29 22:09:22 +03:00
|
|
|
|
2019-06-10 23:58:12 +03:00
|
|
|
{-| -}
|
|
|
|
subscriptions : State -> Sub Msg
|
|
|
|
subscriptions model =
|
|
|
|
Sub.batch
|
|
|
|
[ Sub.map InfoModalMsg (Modal.subscriptions model.infoModal)
|
|
|
|
, Sub.map WarningModalMsg (Modal.subscriptions model.warningModal)
|
|
|
|
]
|