From 5b6af729236ec7f27f64bfca00cc9a149e294beb Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Wed, 2 Jun 2021 12:48:17 +0200 Subject: [PATCH 1/2] Add `embed` API --- src/Element/WithContext.elm | 48 +++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Element/WithContext.elm b/src/Element/WithContext.elm index c4b38b6..154de95 100644 --- a/src/Element/WithContext.elm +++ b/src/Element/WithContext.elm @@ -1,5 +1,5 @@ module Element.WithContext exposing - ( with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr + ( with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr, embed, embedAttr , Element, none, text, el , row, wrappedRow, column , paragraph, textColumn @@ -31,7 +31,7 @@ module Element.WithContext exposing # `elm-ui-with-context` specific functions -@docs with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr +@docs with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr, embed, embedAttr # Basic Elements @@ -369,6 +369,50 @@ attr elem = Attribute <| \_ -> elem +{-| Allow integration with external libraries that require elm-ui `Element`s to be passed in. +-} +embed : + ({ unwrap : Element context msg -> Element.Element msg + , unwrapAttr : Attr context decorative msg -> Element.Attr decorative msg + } + -> Element context msg + ) + -> Element context msg +embed ctor = + Element <| + \context -> + let + (Element result) = + ctor + { unwrap = \(Element f) -> f context + , unwrapAttr = \(Attribute f) -> f context + } + in + result context + + +{-| Allow integration with external libraries that require elm-ui `Element`s to be passed in. +-} +embedAttr : + ({ unwrap : Element context msg -> Element.Element msg + , unwrapAttr : Attr context decorative msg -> Element.Attr decorative msg + } + -> Attr context decorative msg + ) + -> Attr context decorative msg +embedAttr ctor = + Attribute <| + \context -> + let + (Attribute result) = + ctor + { unwrap = \(Element f) -> f context + , unwrapAttr = \(Attribute f) -> f context + } + in + result context + + {-| -} map : (msg -> msg1) -> Element context msg -> Element context msg1 map f (Element g) = From a64f8f90fc660cea25403f78099ea39cfb3a12d8 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 16 Jun 2021 17:30:58 +0200 Subject: [PATCH 2/2] New module 'Embed' for embedding third-party native elm-ui views --- elm.json | 1 + src/Element/WithContext.elm | 48 +--------- src/Element/WithContext/Embed.elm | 103 +++++++++++++++++++++ src/Element/WithContext/Input.elm | 38 ++------ src/Element/WithContext/Input/Internal.elm | 40 ++++++++ 5 files changed, 155 insertions(+), 75 deletions(-) create mode 100644 src/Element/WithContext/Embed.elm create mode 100644 src/Element/WithContext/Input/Internal.elm diff --git a/elm.json b/elm.json index 45e01fc..01b7f87 100644 --- a/elm.json +++ b/elm.json @@ -7,6 +7,7 @@ "exposed-modules": [ "Element.WithContext", "Element.WithContext.Input", + "Element.WithContext.Embed", "Element.WithContext.Events", "Element.WithContext.Background", "Element.WithContext.Border", diff --git a/src/Element/WithContext.elm b/src/Element/WithContext.elm index 154de95..c4b38b6 100644 --- a/src/Element/WithContext.elm +++ b/src/Element/WithContext.elm @@ -1,5 +1,5 @@ module Element.WithContext exposing - ( with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr, embed, embedAttr + ( with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr , Element, none, text, el , row, wrappedRow, column , paragraph, textColumn @@ -31,7 +31,7 @@ module Element.WithContext exposing # `elm-ui-with-context` specific functions -@docs with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr, embed, embedAttr +@docs with, withAttribute, withDecoration, layout, layoutWith, element, attribute, attr # Basic Elements @@ -369,50 +369,6 @@ attr elem = Attribute <| \_ -> elem -{-| Allow integration with external libraries that require elm-ui `Element`s to be passed in. --} -embed : - ({ unwrap : Element context msg -> Element.Element msg - , unwrapAttr : Attr context decorative msg -> Element.Attr decorative msg - } - -> Element context msg - ) - -> Element context msg -embed ctor = - Element <| - \context -> - let - (Element result) = - ctor - { unwrap = \(Element f) -> f context - , unwrapAttr = \(Attribute f) -> f context - } - in - result context - - -{-| Allow integration with external libraries that require elm-ui `Element`s to be passed in. --} -embedAttr : - ({ unwrap : Element context msg -> Element.Element msg - , unwrapAttr : Attr context decorative msg -> Element.Attr decorative msg - } - -> Attr context decorative msg - ) - -> Attr context decorative msg -embedAttr ctor = - Attribute <| - \context -> - let - (Attribute result) = - ctor - { unwrap = \(Element f) -> f context - , unwrapAttr = \(Attribute f) -> f context - } - in - result context - - {-| -} map : (msg -> msg1) -> Element context msg -> Element context msg1 map f (Element g) = diff --git a/src/Element/WithContext/Embed.elm b/src/Element/WithContext/Embed.elm new file mode 100644 index 0000000..3aa906c --- /dev/null +++ b/src/Element/WithContext/Embed.elm @@ -0,0 +1,103 @@ +module Element.WithContext.Embed exposing + ( el, attr + , unwrap, unwrapAttr, unwrapLabel, unwrapOption, unwrapPlaceholder, unwrapThumb + ) + +{-| + +@docs el, attr + +-} + +import Element +import Element.Input +import Element.WithContext.Input.Internal as Internal exposing (Label(..), Option(..), Placeholder(..), Thumb(..), runLabel, runOption, runPlaceholder, runThumb) +import Element.WithContext.Internal as Internal exposing (Attr(..), Element(..), run, runAttr) + + +unwrap = + Internal.run + + +unwrapAttr = + Internal.runAttr + + +unwrapLabel = + Internal.runLabel + + +unwrapOption = + Internal.runOption + + +unwrapPlaceholder = + Internal.runPlaceholder + + +unwrapThumb = + Internal.runThumb + + +{-| Allow integration with external libraries that require elm-ui `Element`s to be passed in. +-} +el : + ({ context : context + , unwrap : Element context msg -> Element.Element msg + , unwrapAttr : Attr context decorative msg -> Element.Attr decorative msg + , unwrapPlaceholder : Placeholder context msg -> Element.Input.Placeholder msg + , unwrapLabel : Label context msg -> Element.Input.Label msg + , unwrapThumb : Thumb context -> Element.Input.Thumb + , unwrapOption : Option context value msg -> Element.Input.Option value msg + } + -> Element context msg + ) + -> Element context msg +el ctor = + Element <| + \context -> + let + (Element result) = + ctor + { context = context + , unwrap = \(Element f) -> f context + , unwrapAttr = \(Attribute f) -> f context + , unwrapPlaceholder = \(Placeholder f) -> f context + , unwrapLabel = \(Label f) -> f context + , unwrapThumb = \(Thumb f) -> f context + , unwrapOption = \(Option f) -> f context + } + in + result context + + +{-| Allow integration with external libraries that require elm-ui `Element`s to be passed in. +-} +attr : + ({ context : context + , unwrap : Element context msg -> Element.Element msg + , unwrapAttr : Attr context decorative msg -> Element.Attr decorative msg + , unwrapPlaceholder : Placeholder context msg -> Element.Input.Placeholder msg + , unwrapLabel : Label context msg -> Element.Input.Label msg + , unwrapThumb : Thumb context -> Element.Input.Thumb + , unwrapOption : Option context value msg -> Element.Input.Option value msg + } + -> Attr context decorative msg + ) + -> Attr context decorative msg +attr ctor = + Attribute <| + \context -> + let + (Attribute result) = + ctor + { context = context + , unwrap = \(Element f) -> f context + , unwrapAttr = \(Attribute f) -> f context + , unwrapPlaceholder = \(Placeholder f) -> f context + , unwrapLabel = \(Label f) -> f context + , unwrapThumb = \(Thumb f) -> f context + , unwrapOption = \(Option f) -> f context + } + in + result context diff --git a/src/Element/WithContext/Input.elm b/src/Element/WithContext/Input.elm index 45b24e4..0137610 100644 --- a/src/Element/WithContext/Input.elm +++ b/src/Element/WithContext/Input.elm @@ -187,12 +187,13 @@ Alternatively, see if it's reasonable to _not_ display an input if you'd normall import Element as Vanilla import Element.Input as Input import Element.WithContext exposing (Attribute, Element, element) +import Element.WithContext.Input.Internal as InputInternal exposing (Label(..), Option(..), Placeholder(..), Thumb(..), runLabel, runOption, runPlaceholder, runThumb) import Element.WithContext.Internal exposing (attribute, attributes, run, wrapAttrs) {-| -} -type Placeholder context msg - = Placeholder (context -> Input.Placeholder msg) +type alias Placeholder context msg = + InputInternal.Placeholder context msg {-| -} @@ -205,8 +206,8 @@ placeholder attrs child = {-| -} -type Label context msg - = Label (context -> Input.Label msg) +type alias Label context msg = + InputInternal.Label context msg buildLabel : @@ -333,9 +334,8 @@ checkbox = ) -{-| -} -type Thumb context - = Thumb (context -> Input.Thumb) +type alias Thumb context = + InputInternal.Thumb context {-| -} @@ -344,11 +344,6 @@ thumb attrs = Thumb <| \context -> Input.thumb <| attributes context attrs -runThumb : a -> Thumb a -> Input.Thumb -runThumb context (Thumb f) = - f context - - {-| -} defaultThumb : Thumb context defaultThumb = @@ -454,16 +449,6 @@ textHelper f = ) -runPlaceholder : context -> Placeholder context msg -> Input.Placeholder msg -runPlaceholder context (Placeholder f) = - f context - - -runLabel : context -> Label context msg -> Input.Label msg -runLabel context (Label f) = - f context - - {-| -} text : List (Attribute context msg) @@ -615,13 +600,8 @@ multiline = {-| -} -type Option context value msg - = Option (context -> Input.Option value msg) - - -runOption : a -> Option a value msg -> Input.Option value msg -runOption context (Option f) = - f context +type alias Option context value msg = + InputInternal.Option context value msg {-| -} diff --git a/src/Element/WithContext/Input/Internal.elm b/src/Element/WithContext/Input/Internal.elm new file mode 100644 index 0000000..ddac293 --- /dev/null +++ b/src/Element/WithContext/Input/Internal.elm @@ -0,0 +1,40 @@ +module Element.WithContext.Input.Internal exposing (Label(..), Option(..), Placeholder(..), Thumb(..), runLabel, runOption, runPlaceholder, runThumb) + +import Element.Input as Input + + +type Placeholder context msg + = Placeholder (context -> Input.Placeholder msg) + + +runPlaceholder : context -> Placeholder context msg -> Input.Placeholder msg +runPlaceholder context (Placeholder f) = + f context + + +type Label context msg + = Label (context -> Input.Label msg) + + +runLabel : context -> Label context msg -> Input.Label msg +runLabel context (Label f) = + f context + + +type Option context value msg + = Option (context -> Input.Option value msg) + + +runOption : a -> Option a value msg -> Input.Option value msg +runOption context (Option f) = + f context + + +{-| -} +type Thumb context + = Thumb (context -> Input.Thumb) + + +runThumb : a -> Thumb a -> Input.Thumb +runThumb context (Thumb f) = + f context