2020-03-31 22:43:32 +03:00
|
|
|
module Examples.Modal exposing (Msg, State, example)
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
|
2020-03-31 22:43:32 +03:00
|
|
|
@docs Msg, State, example
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2019-08-17 01:47:21 +03:00
|
|
|
import Accessibility.Styled as Html exposing (Html, div, h3, h4, p, span, text)
|
2020-06-19 23:41:28 +03:00
|
|
|
import AtomicDesignType exposing (AtomicDesignType(..))
|
2020-09-03 02:07:35 +03:00
|
|
|
import Browser.Dom as Dom
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2018-08-29 22:09:22 +03:00
|
|
|
import Css exposing (..)
|
2020-09-02 23:44:39 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2020-03-31 23:20:03 +03:00
|
|
|
import Example exposing (Example)
|
2019-06-12 21:25:10 +03:00
|
|
|
import Html as Root
|
2020-09-03 02:07:35 +03:00
|
|
|
import Html.Styled.Attributes as Attributes exposing (css)
|
2020-06-20 00:45:32 +03:00
|
|
|
import KeyboardSupport exposing (Direction(..), Key(..))
|
2020-01-29 04:10:20 +03:00
|
|
|
import Nri.Ui.Button.V10 as Button
|
2019-06-11 00:28:38 +03:00
|
|
|
import Nri.Ui.Checkbox.V5 as Checkbox
|
2020-01-31 21:48:48 +03:00
|
|
|
import Nri.Ui.ClickableText.V3 as ClickableText
|
2018-08-29 22:09:22 +03:00
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
2020-09-08 21:29:21 +03:00
|
|
|
import Nri.Ui.FocusTrap.V1 as FocusTrap
|
2020-09-02 20:12:59 +03:00
|
|
|
import Nri.Ui.Modal.V11 as Modal
|
2019-08-17 01:47:21 +03:00
|
|
|
import Nri.Ui.Text.V4 as Text
|
2020-09-03 02:07:35 +03:00
|
|
|
import Task
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2020-07-03 01:09:07 +03:00
|
|
|
{ state : Modal.Model
|
2020-09-04 18:52:47 +03:00
|
|
|
, attributes : Control (List Modal.Attribute)
|
|
|
|
, settings : Control ViewSettings
|
2020-07-03 01:09:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
|
|
|
{ state = Modal.init
|
2020-09-04 18:52:47 +03:00
|
|
|
, attributes = controlAttributes
|
|
|
|
, settings = initViewSettings
|
2020-07-03 01:09:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-04 18:52:47 +03:00
|
|
|
controlAttributes : Control (List Modal.Attribute)
|
|
|
|
controlAttributes =
|
|
|
|
Control.record (\a b c -> a :: b :: c :: [])
|
|
|
|
|> Control.field "Theme" controlTheme
|
|
|
|
|> Control.field "Title visibility" controlTitleVisibility
|
|
|
|
|> Control.field "Custom css" controlCss
|
|
|
|
|
|
|
|
|
|
|
|
type alias ViewSettings =
|
2020-09-03 00:14:04 +03:00
|
|
|
{ title : String
|
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
|
2020-09-02 23:59:02 +03:00
|
|
|
, content : String
|
2019-06-11 00:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-04 18:52:47 +03:00
|
|
|
initViewSettings : Control ViewSettings
|
|
|
|
initViewSettings =
|
|
|
|
Control.record ViewSettings
|
2020-09-03 00:14:04 +03:00
|
|
|
|> Control.field "Modal title" (Control.string "Modal Title")
|
|
|
|
|> Control.field "X button" (Control.bool True)
|
|
|
|
|> Control.field "Continue button" (Control.bool True)
|
|
|
|
|> Control.field "Close button" (Control.bool True)
|
2020-09-02 23:44:39 +03:00
|
|
|
|> Control.field "dismissOnEscAndOverlayClick" (Control.bool True)
|
2020-09-03 00:14:04 +03:00
|
|
|
|> Control.field "Content"
|
2020-09-02 23:59:02 +03:00
|
|
|
(Control.stringTextarea <|
|
|
|
|
String.join "\n\n"
|
|
|
|
[ "Generally, you'll want to pair the Modal.warning theme with the Button.danger theme and the Modal.info theme with the Button.primary theme."
|
|
|
|
, "Muffin liquorice powder liquorice jujubes biscuit cookie candy canes lemon drops. Liquorice powder carrot cake dragée icing tootsie roll apple pie lemon drops lemon drops. Jujubes danish bear claw cotton candy. Dragée apple pie tiramisu. Sugar plum dessert pastry marzipan chocolate cake dragée sesame snaps. Marshmallow gingerbread lemon drops. Brownie chocolate fruitcake pastry. Powder jelly beans jujubes. Croissant macaroon dessert cookie candy canes jelly jujubes. Muffin liquorice ice cream wafer donut danish soufflé dragée chocolate bar. Candy croissant candy wafer toffee lemon drops croissant danish sugar plum. Cookie cake candy canes. Pastry powder muffin soufflé tootsie roll sweet cookie tiramisu."
|
|
|
|
, "Candy cake danish gingerbread. Caramels toffee cupcake toffee sweet. Gummi bears candy cheesecake sweet. Pie gingerbread sugar plum halvah muffin icing marzipan wafer icing. Candy fruitcake gummies icing marzipan. Halvah jelly beans candy candy canes biscuit bonbon sesame snaps. Biscuit carrot cake croissant cake chocolate lollipop candy biscuit croissant. Topping jujubes apple pie croissant chocolate cake. Liquorice cookie dragée gummies cotton candy fruitcake lemon drops candy canes. Apple pie lemon drops gummies cake chocolate bar cake jelly-o tiramisu. Chocolate bar icing pudding marshmallow cake soufflé soufflé muffin. Powder lemon drops biscuit sugar plum cupcake carrot cake powder cake dragée. Bear claw gummi bears liquorice sweet roll."
|
|
|
|
]
|
|
|
|
)
|
2020-09-02 23:53:40 +03:00
|
|
|
|
|
|
|
|
2020-09-03 00:04:06 +03:00
|
|
|
controlTitleVisibility : Control Modal.Attribute
|
|
|
|
controlTitleVisibility =
|
|
|
|
Control.choice
|
|
|
|
[ ( "showTitle", Control.value Modal.showTitle )
|
|
|
|
, ( "hideTitle", Control.value Modal.hideTitle )
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-02 23:53:40 +03:00
|
|
|
controlTheme : Control Modal.Attribute
|
|
|
|
controlTheme =
|
|
|
|
Control.choice
|
|
|
|
[ ( "info", Control.value Modal.info )
|
|
|
|
, ( "warning", Control.value Modal.warning )
|
|
|
|
]
|
2018-08-29 22:09:22 +03:00
|
|
|
|
|
|
|
|
2020-09-03 00:08:40 +03:00
|
|
|
controlCss : Control Modal.Attribute
|
|
|
|
controlCss =
|
|
|
|
Control.map Modal.css <|
|
|
|
|
Control.choice
|
|
|
|
[ ( "[]", Control.value [] )
|
|
|
|
, ( "[ Css.borderRadius Css.zero ]", Control.value [ Css.borderRadius Css.zero ] )
|
|
|
|
, ( "[ Css.width (Css.px 900) ]", Control.value [ Css.width (Css.px 900) ] )
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-08-29 22:09:22 +03:00
|
|
|
{-| -}
|
2020-03-31 23:20:03 +03:00
|
|
|
example : Example State Msg
|
2020-03-31 22:43:32 +03:00
|
|
|
example =
|
2020-09-09 21:43:10 +03:00
|
|
|
{ name = "Modal"
|
|
|
|
, version = 11
|
2020-03-31 22:43:32 +03:00
|
|
|
, categories = [ Modals ]
|
2020-06-20 00:16:10 +03:00
|
|
|
, atomicDesignType = Organism
|
2020-09-03 00:19:41 +03:00
|
|
|
, keyboardSupport =
|
|
|
|
[ { keys = [ KeyboardSupport.Tab ]
|
|
|
|
, result = "Moves focus to the next button within the modal or wraps back to the first element within the modal."
|
|
|
|
}
|
|
|
|
, { keys = [ KeyboardSupport.Tab, KeyboardSupport.Shift ]
|
|
|
|
, result = "Moves focus to the previous button within the modal or wraps back to the last element within the modal."
|
|
|
|
}
|
|
|
|
, { keys = [ KeyboardSupport.Esc ]
|
|
|
|
, result = "If 'dismissOnEscAndOverlayClick' is set to true, closes the modal. Else, does nothing."
|
|
|
|
}
|
|
|
|
]
|
2020-03-31 22:43:32 +03:00
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-03-31 22:48:26 +03:00
|
|
|
, subscriptions = subscriptions
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
|
|
|
\state ->
|
2020-08-12 17:15:23 +03:00
|
|
|
let
|
2020-09-02 23:44:39 +03:00
|
|
|
settings =
|
|
|
|
Control.currentValue state.settings
|
2020-08-12 17:15:23 +03:00
|
|
|
in
|
2020-09-04 18:52:47 +03:00
|
|
|
[ div [ css [ Css.displayFlex, Css.justifyContent Css.spaceAround ] ]
|
|
|
|
[ Control.view UpdateAttributes state.attributes
|
|
|
|
|> Html.fromUnstyled
|
|
|
|
, Control.view UpdateSettings state.settings
|
|
|
|
|> Html.fromUnstyled
|
|
|
|
]
|
2020-09-03 02:07:35 +03:00
|
|
|
, launchModalButton settings
|
2020-09-05 03:23:41 +03:00
|
|
|
, Modal.view
|
|
|
|
{ title = settings.title
|
|
|
|
, wrapMsg = ModalMsg
|
|
|
|
, content = [ viewModalContent settings.content ]
|
|
|
|
, footer =
|
|
|
|
List.filterMap identity
|
|
|
|
[ if settings.showContinue then
|
|
|
|
Just continueButton
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
, if settings.showSecondary then
|
|
|
|
Just closeClickableText
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
]
|
2020-09-08 21:34:46 +03:00
|
|
|
, focusTrap =
|
|
|
|
{ focus = Focus
|
|
|
|
, firstId =
|
|
|
|
if settings.showX then
|
|
|
|
Modal.closeButtonId
|
|
|
|
|
|
|
|
else if settings.showContinue then
|
|
|
|
continueButtonId
|
|
|
|
|
|
|
|
else
|
|
|
|
closeClickableTextId
|
|
|
|
, lastId =
|
|
|
|
if settings.showSecondary then
|
|
|
|
closeClickableTextId
|
|
|
|
|
|
|
|
else if settings.showContinue then
|
|
|
|
continueButtonId
|
|
|
|
|
|
|
|
else
|
|
|
|
Modal.closeButtonId
|
|
|
|
}
|
2020-09-05 01:36:22 +03:00
|
|
|
}
|
2020-09-08 21:34:46 +03:00
|
|
|
(List.concat
|
|
|
|
[ if settings.showX then
|
2020-09-05 02:59:02 +03:00
|
|
|
[ Modal.closeButton ]
|
|
|
|
|
|
|
|
else
|
|
|
|
[]
|
2020-09-05 01:36:22 +03:00
|
|
|
, Control.currentValue state.attributes
|
|
|
|
]
|
|
|
|
)
|
2020-09-04 18:52:47 +03:00
|
|
|
state.state
|
2019-07-26 08:14:26 +03:00
|
|
|
]
|
2018-08-29 22:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-04 18:52:47 +03:00
|
|
|
launchModalButton : ViewSettings -> Html Msg
|
2020-09-03 02:07:35 +03:00
|
|
|
launchModalButton settings =
|
|
|
|
let
|
|
|
|
launchId =
|
|
|
|
"launch-modal"
|
|
|
|
|
|
|
|
startFocusId =
|
|
|
|
if settings.showContinue then
|
|
|
|
Just continueButtonId
|
|
|
|
|
|
|
|
else if settings.showX then
|
|
|
|
Just Modal.closeButtonId
|
|
|
|
|
|
|
|
else if settings.showSecondary then
|
|
|
|
Just closeClickableTextId
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
in
|
|
|
|
Button.button "Launch Modal"
|
|
|
|
[ case startFocusId of
|
|
|
|
Just autofocusElementId ->
|
|
|
|
Button.onClick
|
|
|
|
(OpenModal
|
|
|
|
{ startFocusOn = autofocusElementId
|
|
|
|
, returnFocusTo = launchId
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Button.disabled
|
|
|
|
, Button.custom [ Attributes.id launchId ]
|
|
|
|
, Button.secondary
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-02 23:59:02 +03:00
|
|
|
viewModalContent : String -> Html msg
|
|
|
|
viewModalContent content =
|
2020-09-03 02:07:35 +03:00
|
|
|
Text.mediumBody [ span [ css [ whiteSpace preLine ] ] [ text content ] ]
|
|
|
|
|
|
|
|
|
|
|
|
continueButtonId : String
|
|
|
|
continueButtonId =
|
|
|
|
"continue-button-id"
|
2019-08-17 01:47:21 +03:00
|
|
|
|
|
|
|
|
2020-09-05 02:49:07 +03:00
|
|
|
continueButton : Html Msg
|
|
|
|
continueButton =
|
2020-09-03 00:35:35 +03:00
|
|
|
Button.button "Continue"
|
|
|
|
[ Button.premium
|
2020-09-03 02:37:00 +03:00
|
|
|
, Button.onClick CloseModal
|
2020-09-05 02:49:07 +03:00
|
|
|
, Button.custom [ Attributes.id continueButtonId ]
|
2020-09-03 00:35:35 +03:00
|
|
|
, Button.large
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-03 02:07:35 +03:00
|
|
|
closeClickableTextId : String
|
|
|
|
closeClickableTextId =
|
2020-09-05 03:15:54 +03:00
|
|
|
"close-clickable-text-id"
|
2020-09-03 02:07:35 +03:00
|
|
|
|
|
|
|
|
2020-09-05 02:49:07 +03:00
|
|
|
closeClickableText : Html Msg
|
|
|
|
closeClickableText =
|
2020-09-03 00:32:57 +03:00
|
|
|
ClickableText.button "Close"
|
2020-09-03 02:37:00 +03:00
|
|
|
[ ClickableText.onClick CloseModal
|
2020-09-03 00:32:57 +03:00
|
|
|
, ClickableText.large
|
2020-09-05 02:49:07 +03:00
|
|
|
, ClickableText.custom [ Attributes.id closeClickableTextId ]
|
2020-09-03 00:32:57 +03:00
|
|
|
, ClickableText.css [ Css.marginTop (Css.px 15) ]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-08-29 22:09:22 +03:00
|
|
|
{-| -}
|
2019-06-11 00:28:38 +03:00
|
|
|
type Msg
|
2020-09-03 02:07:35 +03:00
|
|
|
= OpenModal { startFocusOn : String, returnFocusTo : String }
|
2020-07-03 01:33:51 +03:00
|
|
|
| ModalMsg Modal.Msg
|
2020-09-03 02:37:00 +03:00
|
|
|
| CloseModal
|
2020-09-04 18:52:47 +03:00
|
|
|
| UpdateAttributes (Control (List Modal.Attribute))
|
|
|
|
| UpdateSettings (Control ViewSettings)
|
2020-09-03 02:07:35 +03:00
|
|
|
| Focus String
|
|
|
|
| Focused (Result Dom.Error ())
|
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
|
2020-07-03 01:09:07 +03:00
|
|
|
settings =
|
|
|
|
state.settings
|
|
|
|
|
2019-06-12 21:12:37 +03:00
|
|
|
updateConfig =
|
2020-09-02 23:44:39 +03:00
|
|
|
{ dismissOnEscAndOverlayClick =
|
|
|
|
(Control.currentValue settings).dismissOnEscAndOverlayClick
|
|
|
|
}
|
2019-06-12 21:12:37 +03:00
|
|
|
in
|
2018-08-29 22:09:22 +03:00
|
|
|
case msg of
|
2020-09-03 02:07:35 +03:00
|
|
|
OpenModal config ->
|
2020-09-02 20:34:12 +03:00
|
|
|
let
|
|
|
|
( newState, cmd ) =
|
2020-09-03 02:07:35 +03:00
|
|
|
Modal.open config
|
2020-09-02 20:34:12 +03:00
|
|
|
in
|
2020-09-02 23:53:40 +03:00
|
|
|
( { state | state = newState }
|
2020-09-02 20:34:12 +03:00
|
|
|
, Cmd.map ModalMsg cmd
|
|
|
|
)
|
2020-07-03 01:33:51 +03:00
|
|
|
|
2020-07-03 01:09:07 +03:00
|
|
|
ModalMsg modalMsg ->
|
|
|
|
case Modal.update updateConfig modalMsg state.state of
|
2020-09-02 20:34:12 +03:00
|
|
|
( newState, cmd ) ->
|
2020-07-03 01:09:07 +03:00
|
|
|
( { state | state = newState }
|
2020-09-02 20:34:12 +03:00
|
|
|
, Cmd.map ModalMsg cmd
|
2019-06-10 23:58:12 +03:00
|
|
|
)
|
2018-08-29 22:09:22 +03:00
|
|
|
|
2020-09-03 02:37:00 +03:00
|
|
|
CloseModal ->
|
2020-09-02 20:34:12 +03:00
|
|
|
let
|
|
|
|
( newState, cmd ) =
|
|
|
|
Modal.close state.state
|
|
|
|
in
|
|
|
|
( { state | state = newState }
|
|
|
|
, Cmd.map ModalMsg cmd
|
2019-06-11 03:12:35 +03:00
|
|
|
)
|
|
|
|
|
2020-09-04 18:52:47 +03:00
|
|
|
UpdateAttributes value ->
|
|
|
|
( { state | attributes = value }, Cmd.none )
|
|
|
|
|
2020-09-02 23:44:39 +03:00
|
|
|
UpdateSettings value ->
|
|
|
|
( { state | settings = value }, Cmd.none )
|
2020-08-12 17:15:23 +03:00
|
|
|
|
2020-09-03 02:07:35 +03:00
|
|
|
Focus id ->
|
|
|
|
( state, Task.attempt Focused (Dom.focus id) )
|
|
|
|
|
|
|
|
Focused _ ->
|
|
|
|
( state, Cmd.none )
|
|
|
|
|
2018-08-29 22:09:22 +03:00
|
|
|
|
2019-06-10 23:58:12 +03:00
|
|
|
{-| -}
|
|
|
|
subscriptions : State -> Sub Msg
|
|
|
|
subscriptions model =
|
2020-07-03 01:09:07 +03:00
|
|
|
Sub.map ModalMsg (Modal.subscriptions model.state)
|