From c56bfcddcc31d9256b5ba18a06ec401b27640649 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 12 May 2021 12:31:46 +0200 Subject: [PATCH 1/6] Expose a 'run' function When wrapping a third-party view that takes native elm-ui elements, we must be able to render a with-context element into a native element. --- src/Element/WithContext.elm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Element/WithContext.elm b/src/Element/WithContext.elm index c4b38b6..c3fb667 100644 --- a/src/Element/WithContext.elm +++ b/src/Element/WithContext.elm @@ -24,6 +24,7 @@ module Element.WithContext exposing , map, mapAttribute , html, htmlAttribute , withContext, withContextAttribute, withContextDecoration + , run ) {-| @@ -227,7 +228,7 @@ Sometimes it's more convenient to just access the whole context while building y -} import Element -import Element.WithContext.Internal as Internal exposing (Attr(..), Attribute, Element(..), attr, attribute, attributes, run, runAttr, wrapAttrs, wrapContainer) +import Element.WithContext.Internal as Internal exposing (Attr(..), Attribute, Element(..), attr, attribute, attributes, runAttr, wrapAttrs, wrapContainer) import Html exposing (Html) @@ -423,6 +424,14 @@ withContextDecoration f = Attribute <| \context -> runAttr context <| f context +{-| Change a `Element context msg` into a `Element.Element msg`, which is +needed when wrapping a third-party view that takes non-WithContext elements +-} +run : context -> Element context msg -> Element.Element msg +run = + Internal.run + + {-| -} type alias Length = Element.Length From 14165de6f04dcfc26ae4a4bf1ea3c507b57c494c Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Thu, 13 May 2021 00:19:21 +0200 Subject: [PATCH 2/6] Input: export runPlaceholder --- src/Element/WithContext/Input.elm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Element/WithContext/Input.elm b/src/Element/WithContext/Input.elm index 45b24e4..6a35ead 100644 --- a/src/Element/WithContext/Input.elm +++ b/src/Element/WithContext/Input.elm @@ -3,7 +3,7 @@ module Element.WithContext.Input exposing , button , checkbox, defaultCheckbox , text, multiline - , Placeholder, placeholder + , Placeholder, placeholder, runPlaceholder , username, newPassword, currentPassword, email, search, spellChecked , slider, Thumb, thumb, defaultThumb , radio, radioRow, Option, option, optionWith, OptionState(..) @@ -66,7 +66,7 @@ This is also the first input element that has a [`required label`](#Label). @docs text, multiline -@docs Placeholder, placeholder +@docs Placeholder, placeholder, runPlaceholder ## Text with autofill From 8ad7eae1131ea1c6ec7dbb9276bee5118f5c5b32 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Mon, 24 May 2021 13:50:17 +0200 Subject: [PATCH 3/6] expose 'runAttr' --- src/Element/WithContext.elm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Element/WithContext.elm b/src/Element/WithContext.elm index c3fb667..1d7ef5e 100644 --- a/src/Element/WithContext.elm +++ b/src/Element/WithContext.elm @@ -24,7 +24,7 @@ module Element.WithContext exposing , map, mapAttribute , html, htmlAttribute , withContext, withContextAttribute, withContextDecoration - , run + , run, runAttr ) {-| @@ -228,10 +228,14 @@ Sometimes it's more convenient to just access the whole context while building y -} import Element -import Element.WithContext.Internal as Internal exposing (Attr(..), Attribute, Element(..), attr, attribute, attributes, runAttr, wrapAttrs, wrapContainer) +import Element.WithContext.Internal as Internal exposing (Attr(..), Attribute, Element(..), attr, attribute, attributes, wrapAttrs, wrapContainer) import Html exposing (Html) +runAttr = + Internal.runAttr + + {-| -} type alias Color = Internal.Color From 35fdd871826aba50cad40dd4885d01e526c8af81 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 26 May 2021 10:18:42 +0200 Subject: [PATCH 4/6] add a 'withAttr' function --- src/Element/WithContext.elm | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Element/WithContext.elm b/src/Element/WithContext.elm index 1d7ef5e..5800b82 100644 --- a/src/Element/WithContext.elm +++ b/src/Element/WithContext.elm @@ -24,7 +24,7 @@ module Element.WithContext exposing , map, mapAttribute , html, htmlAttribute , withContext, withContextAttribute, withContextDecoration - , run, runAttr + , run, runAttr, withAttr ) {-| @@ -407,6 +407,13 @@ withDecoration selector f = Attribute <| \context -> runAttr context <| f <| selector context +{-| Use a property from the context to build an `Attr`. Have a look at the README for examples. +-} +withAttr : (context -> property) -> (property -> Attr context decoration msg) -> Attr context decoration msg +withAttr selector f = + Attribute <| \context -> runAttr context <| f <| selector context + + {-| Use the context to build an `Element`. Have a look at the README for examples. -} withContext : (context -> Element context msg) -> Element context msg From feabde7aaeb81e1f2a90e9ac8f8b09404c55abb8 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 1 Jun 2021 13:50:03 +0200 Subject: [PATCH 5/6] expose 'runLabel' and 'attributes' --- src/Element/WithContext.elm | 8 ++++++-- src/Element/WithContext/Input.elm | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Element/WithContext.elm b/src/Element/WithContext.elm index 5800b82..335f8f2 100644 --- a/src/Element/WithContext.elm +++ b/src/Element/WithContext.elm @@ -24,7 +24,7 @@ module Element.WithContext exposing , map, mapAttribute , html, htmlAttribute , withContext, withContextAttribute, withContextDecoration - , run, runAttr, withAttr + , attributes, run, runAttr, withAttr ) {-| @@ -228,7 +228,7 @@ Sometimes it's more convenient to just access the whole context while building y -} import Element -import Element.WithContext.Internal as Internal exposing (Attr(..), Attribute, Element(..), attr, attribute, attributes, wrapAttrs, wrapContainer) +import Element.WithContext.Internal as Internal exposing (Attr(..), Attribute, Element(..), attr, attribute, wrapAttrs, wrapContainer) import Html exposing (Html) @@ -236,6 +236,10 @@ runAttr = Internal.runAttr +attributes = + Internal.attributes + + {-| -} type alias Color = Internal.Color diff --git a/src/Element/WithContext/Input.elm b/src/Element/WithContext/Input.elm index 6a35ead..0b15a64 100644 --- a/src/Element/WithContext/Input.elm +++ b/src/Element/WithContext/Input.elm @@ -3,7 +3,7 @@ module Element.WithContext.Input exposing , button , checkbox, defaultCheckbox , text, multiline - , Placeholder, placeholder, runPlaceholder + , Placeholder, placeholder, runPlaceholder, runLabel , username, newPassword, currentPassword, email, search, spellChecked , slider, Thumb, thumb, defaultThumb , radio, radioRow, Option, option, optionWith, OptionState(..) @@ -66,7 +66,7 @@ This is also the first input element that has a [`required label`](#Label). @docs text, multiline -@docs Placeholder, placeholder, runPlaceholder +@docs Placeholder, placeholder, runPlaceholder, runLabel ## Text with autofill From 9b41df041a0cb961d696a8608d5416bee6090441 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Wed, 2 Jun 2021 12:48:17 +0200 Subject: [PATCH 6/6] 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) =