|
| 1 | +module Lumi.Components2.Clip where |
| 2 | + |
| 3 | +import Prelude |
| 4 | +import Data.Foldable (for_) |
| 5 | +import Data.Monoid (guard) |
| 6 | +import Data.Newtype (class Newtype) |
| 7 | +import Data.Nullable (Nullable) |
| 8 | +import Data.Nullable as Nullable |
| 9 | +import Effect (Effect) |
| 10 | +import Effect.Aff (Error, Milliseconds(..), delay, message) |
| 11 | +import Effect.Class (liftEffect) |
| 12 | +import Effect.Console as Console |
| 13 | +import Effect.Uncurried (EffectFn1, EffectFn3, mkEffectFn1, runEffectFn3) |
| 14 | +import Effect.Unsafe (unsafePerformEffect) |
| 15 | +import Lumi.Components (LumiComponent, lumiComponent, lumiElement) |
| 16 | +import Lumi.Components.Spacing (Space(..)) |
| 17 | +import Lumi.Components2.Box (box) |
| 18 | +import Lumi.Components2.Button (_linkStyle, button) |
| 19 | +import Lumi.Styles (styleModifier_, toCSS) |
| 20 | +import Lumi.Styles.Box (FlexAlign(..), _justify) |
| 21 | +import Lumi.Styles.Box as Styles.Box |
| 22 | +import Lumi.Styles.Clip as Styles.Clip |
| 23 | +import Lumi.Styles.Theme (LumiTheme(..), useTheme) |
| 24 | +import React.Basic.DOM as R |
| 25 | +import React.Basic.Emotion as E |
| 26 | +import React.Basic.Hooks (Hook, JSX, Ref, UseState, coerceHook, readRefMaybe, useRef, useState, (/\), type (/\)) |
| 27 | +import React.Basic.Hooks as React |
| 28 | +import React.Basic.Hooks.Aff (UseAff, useAff) |
| 29 | +import React.Basic.Hooks.ResetToken (ResetToken, UseResetToken, useResetToken) |
| 30 | +import Web.DOM (Node) |
| 31 | + |
| 32 | +type ClipProps |
| 33 | + = ( content :: Array JSX |
| 34 | + ) |
| 35 | + |
| 36 | +clip :: LumiComponent ClipProps |
| 37 | +clip = |
| 38 | + unsafePerformEffect do |
| 39 | + lumiComponent "Clip" defaults \props -> React.do |
| 40 | + theme@(LumiTheme { colors }) <- useTheme |
| 41 | + ref <- useRef Nullable.null |
| 42 | + { copied, copy } <- useClip ref |
| 43 | + let |
| 44 | + copyButton = |
| 45 | + lumiElement button |
| 46 | + $ _linkStyle |
| 47 | + $ styleModifier_ |
| 48 | + ( E.merge |
| 49 | + [ E.css |
| 50 | + { marginLeft: E.prop S16 |
| 51 | + , lineHeight: E.str "12px" |
| 52 | + } |
| 53 | + , guard copied do |
| 54 | + E.css |
| 55 | + { color: E.color colors.black1 |
| 56 | + , "&:hover": E.nested $ E.css { textDecoration: E.none } |
| 57 | + } |
| 58 | + ] |
| 59 | + ) |
| 60 | + $ _ |
| 61 | + { content = [ R.text if copied then "Copied!" else "Copy" ] |
| 62 | + , onPress = copy |
| 63 | + } |
| 64 | + pure |
| 65 | + $ E.element R.div' |
| 66 | + { className: props.className |
| 67 | + , css: toCSS theme props Styles.Clip.clip |
| 68 | + , children: |
| 69 | + [ E.element R.div' |
| 70 | + { className: "" |
| 71 | + , css: |
| 72 | + toCSS theme props |
| 73 | + $ Styles.Box.box |
| 74 | + >>> Styles.Box._justify Center |
| 75 | + , ref |
| 76 | + , children: props.content |
| 77 | + } |
| 78 | + , lumiElement box |
| 79 | + $ _justify Center |
| 80 | + $ _ { content = [ copyButton ] } |
| 81 | + ] |
| 82 | + } |
| 83 | + where |
| 84 | + defaults = { content: [] } |
| 85 | + |
| 86 | +newtype UseClip hooks |
| 87 | + = UseClip (UseAff (ResetToken /\ Boolean) Unit (UseState Boolean (UseResetToken hooks))) |
| 88 | + |
| 89 | +derive instance ntUseClip :: Newtype (UseClip hooks) _ |
| 90 | + |
| 91 | +useClip :: Ref (Nullable Node) -> Hook UseClip { copied :: Boolean, copy :: Effect Unit } |
| 92 | +useClip nodeRef = |
| 93 | + coerceHook React.do |
| 94 | + token /\ resetToken <- useResetToken |
| 95 | + copied /\ setCopied <- useState false |
| 96 | + let |
| 97 | + copy = do |
| 98 | + node <- readRefMaybe nodeRef |
| 99 | + for_ node |
| 100 | + $ runEffectFn3 copyNodeContents |
| 101 | + ( do |
| 102 | + setCopied \_ -> true |
| 103 | + resetToken |
| 104 | + ) |
| 105 | + (mkEffectFn1 $ Console.error <<< message) |
| 106 | + useAff (token /\ copied) do |
| 107 | + when copied do |
| 108 | + delay $ Milliseconds 5000.0 |
| 109 | + liftEffect $ setCopied \_ -> false |
| 110 | + pure { copied, copy } |
| 111 | + |
| 112 | +foreign import copyNodeContents :: EffectFn3 (Effect Unit) (EffectFn1 Error Unit) Node Unit |
0 commit comments