on eac@fix-hydration-skip-to-replace-comment help sign in
reflex-dom/Quickref.md
159 lines 7.0 kB history raw
1# Reflex-Dom Quick(ish) Reference
3## Typeclasses
5Function signatures have been simplified. The 't' type parameter and many typeclass constraints are removed, and annotations are added regarding the monad of the result type (See the Reflex Quickstart for more discussion). Annotations:
7```haskell
8[ ] -- Pure function
9[W] -- Function runs in any monad supporting MonadWidget
10[I] -- Function runs in IO
11```
13Functions from Reflex that run in MonadSample or MonadHold context ([S] or [H]) will also run in [W].
15## Creating widgets (DOM elements hooked into the FRP system)
17### Basic widgets
19These functions generally take an element tag (such as "div", "h1", etc) and a child widget, and produce a widget of the given type containing the child. Some of these functions return data of type `El`, which is a handle to the created DOM element along with a set of potential Events that might occur on that element.
21Widgets may return any type (this is 'a' in many of the functions below). Often this will be an Event carrying user interactions with the widget.
23```haskell
24-- Simplest form. Create a widget of given type containing the given child.
25-- Return whatever the child returns.
26[W] el :: Text -> m a -> m a
28-- This version returns the 'El' as well.
29[W] el' :: Text -> m a -> m (El, a)
31-- These two additionally apply attributes to the element, such as ("class" =: "blah"). (=:) is 'Map.singleton'
32[W] elAttr :: Text -> Map Text Text -> m a -> m a
33[W] elAttr' :: Text -> Map Text Text -> m a -> m (El, a)
35-- As above, but now the attribute map is Dynamic
36[W] elDynAttr :: Text -> Dynamic (Map Text Text) -> m a -> m a
37[W] elDynAttr' :: Text -> Dynamic (Map Text Text) -> m a -> m (El, a)
39-- As above, but with an optional XML namespace for the tag. Note that this does *not* set the 'xmlns' attribute. See https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-DocCrElNS
40[W] elDynAttrNS' :: Maybe Text -> Text -> Dynamic (Map Text Text) -> m a -> m (El, a)
42-- Shortcut for elAttr when you only want to set the "class" attribute.
43[W] elClass :: Text -> Text -> m a -> m a
45-- Even shorter-cut for above when element type is "div". Create a div of given class.
46[W] divClass :: Text -> m a -> m a
48-- Create a widget of given type with arbitrary, Dynamic HTML inside.
49[W] elDynHtml' :: Text -> Dynamic Text -> m El
50[W] elDynHtmlAttr' :: Text -> Map Text Text -> Dynamic Text -> m El
52-- Create a static text element
53[W] text :: Text -> m ()
55-- Create a dynamic text element
56[W] dynText :: Dynamic Text -> m ()
57[W] display :: Show a => Dynamic a -> m ()
59-- Create a "button" element with given label, return onClick Event
60[W] button :: Text -> m (Event ())
62-- Empty widget
63[W] blank :: m ()
64```
66### Dynamic widgets
68In the Dynamic cases so far, the *content* of a widget is dynamic but the *definition* of the widget is static. The functions below enable the definition and/or structure of the widget itself to change over time.
70Note the "list" functions do not imply particular HTML tags (ul, li, etc), though the widgets they create can have those tags if you construct them appropriately.
72```haskell
73-- Create a dynamically-redefined widget from a Dynamic of widget actions.
74[W] dyn :: Dynamic (m a) -> m (Event a)
76-- Same as dyn, but takes initial value and an update Event instead of a Dynamic.
77[W] widgetHold :: m a -> Event (m a) -> m (Dynamic a)
78```
80Also see the "Collection management functions" section in the `reflex` Quick Reference.
82### Utility widgets
84These are useful widgets that are implemented (or could be implemented) in terms of the low-level widgets above.
86Some of these widget builders take a configuration record and return a record containing Events or other useful data associated with the created widget (similar to 'El'). The configuration records have default values, so you can just supply 'def'. See Reflex/Dom/Widget/Input.hs for record fields (Lenses are provided).
88```haskell
89-- Text input.
90[W] textInput :: TextInputConfig -> m TextInput
91[ ] textInputGetEnter :: TextInput -> Event ()
92[W] textArea :: TextAreaConfig -> m TextArea
94-- Range input (slider with float values).
95[W] rangeInput :: RangeInputConfig -> m RangeInput
97-- Checkbox. The Bool supplies the initial state.
98[W] checkbox :: Bool -> CheckboxConfig -> m Checkbox
100-- Dropdown with Dynamic options. First argument is initial state.
101[W] dropdown :: (Ord k, Show k, Read k) =>
102 k -> Dynamic (Map k Text) -> DropdownConfig k -> m (Dropdown k)
104-- Table with static columns and dynamic rows.
105[W] tableDynAttr :: ... -- See Reflex.Dom.Widget.Basic
107-- Tabbed view that shows only one of its child widgets at a time.
108[W] tabDisplay :: (Show k, Ord k) => Text -> Text -> Map k (Text, m ()) -> m ()
110-- Widget to efficiently display long scrolling lists.
111[W] virtualListWithSelection :: ... -- See Reflex.Dom.Widget.Lazy
112```
114## Connecting to the real world (I/O)
116```haskell
117-- Extract the specified Event from an 'El'. See Reflex.Dom.Widget.Basic
118[ ] domEvent :: EventName en -> El -> Event (EventResultType en)
119```
121Also see the "Connection to the real world" and "Time" sections in the `reflex` Quick Reference.
123### XMLHttpRequest
125Convenience functions for XMLHttpRequest. see Reflex.Dom.Xhr
127```haskell
128-- Given method, URL, and config record (with default instance), construct a request.
129[ ] xhrRequest :: Text -> Text -> XhrRequestConfig a -> XhrRequest a
131-- Given Event of requests, issue them and produce Event of responses.
132[W] performRequestAsync :: Event (XhrRequest a) -> m (Event XhrResponse)
134-- Issue a collection of requests, wait for them ALL to complete, return collected results.
135[W] performRequestsAsync :: Traversable f => Event (f (XhrRequest a)) -> m (Event (f XhrResponse))
137-- Convenience function to decode JSON-encoded responses.
138[ ] decodeXhrResponse :: FromJSON a => XhrResponse -> Maybe a
140-- Simplified interface to "GET" URLs and return decoded results.
141[W] getAndDecode :: FromJSON a => Event Text -> m (Event (Maybe a))
142```
144## Startup
146```haskell
147-- Reflex-Dom entry point. Takes a monadic widget-building action of lengthy
148-- type and turns it into an IO action.
149[I] mainWidget ::
150 Widget Spider (Gui Spider (WithWebView SpiderHost) (HostFrame Spider)) () -> IO ()
151[I] mainWidgetWithHead ::
152 Widget Spider (Gui Spider (WithWebView SpiderHost) (HostFrame Spider)) () ->
153 Widget Spider (Gui Spider (WithWebView SpiderHost) (HostFrame Spider)) () -> IO ()
154[I] mainWidgetWithCss ::
155 ByteString ->
156 Widget Spider (Gui Spider (WithWebView SpiderHost) (HostFrame Spider)) () -> IO ()
157```
159Also see the corresponding section in the `reflex` Quick Reference.