add CopyButton#662
Conversation
|
Your second - and third I guess - copy-to-clipboard components plotly/dash-core-components#932 -> https://dash.plotly.com/dash-core-components/clipboard 😎 These ones have a little different API than the one in DCC. Which I guess flows from the way Mantine does things, but I'm worried that this way is harder to use in a Dash context (particularly for the callback-updated-value use case - is it possible to do this within the one component or does it have to be a separate button like in your example? If not, what would you do if you ONLY want the callback value, hide the Changing to a pattern more like |
|
Haha - that link to the Diverging with the Mantine API is ok if it's for parity with Dash. I think the feature of being able to specify a The Mantine It's possible to update the value in a single callback as in the example below. The import dash_mantine_components as dmc
from dash import Dash, Input, Output, no_update
from dash_iconify import DashIconify
app = Dash()
component = dmc.CopyButton(
value="",
children=DashIconify(icon="tabler:clipboard"),
copiedChildren=DashIconify(icon="tabler:check"),
color="blue",
copiedColor="teal",
n_clicks=0,
id="copy"
)
app.layout = dmc.MantineProvider(dmc.Box([
dmc.Text("CopyButton demo"),
component,
], m="lg")
)
@app.callback(
Output("copy", "value"),
Output("copy", "triggerCopy"),
Input("copy", "n_clicks"),
)
def update(n):
if n > 0:
return str(n) + "update", True
return no_update, no_update
if __name__ == "__main__":
app.run(debug=True)
|
|
Oops. Looks like I spoke too soon. I tried adding the I think this is still good to go as-is and I can open a feature request for the Here is an example of copying the contents of a dmc.Textarea to the clipboard import dash_mantine_components as dmc
from dash import Dash, Input, Output, State, no_update
from dash_iconify import DashIconify
app = Dash()
component = dmc.CopyButton(
value="",
children=DashIconify(icon="tabler:clipboard"),
copiedChildren=DashIconify(icon="tabler:check"),
color="blue",
copiedColor="teal",
n_clicks=0,
id="copy"
)
app.layout = dmc.MantineProvider(dmc.Box([
dmc.Text("CopyButton demo"),
component,
dmc.Textarea(id="textarea")
], m="lg")
)
@app.callback(
Output("copy", "value"),
Output("copy", "triggerCopy"),
Input("copy", "n_clicks"),
State("textarea", "value")
)
def update(n, val):
if n > 0:
return val, True
return no_update, no_update
if __name__ == "__main__":
app.run(debug=True)
|
|
Well, might have found a solution for the target_id. This works correctly now: import dash_mantine_components as dmc
from dash import Dash
from dash_iconify import DashIconify
app = Dash()
component = dmc.CopyButton(
value="",
children=DashIconify(icon="tabler:clipboard"),
copiedChildren=DashIconify(icon="tabler:check"),
color="blue",
copiedColor="teal",
n_clicks=0,
id="copy",
target_id="textarea"
)
app.layout = dmc.MantineProvider(dmc.Box([
dmc.Text("CopyButton demo"),
component,
dmc.Textarea(id="textarea")
], m="lg")
)
if __name__ == "__main__":
app.run(debug=True)
|
alexcjohnson
left a comment
There was a problem hiding this comment.
Clever solution to get target_id to work 🎉
Add Copy-to-Clipboard Components
This PR adds two components for creating copy-to-clipboard buttons with Mantine styles.
1.
CopyButtonA ready-to-use copy button built with the Mantine
Buttoncomponent. Supports props for dynamic text, icons and colors while copying:children,copiedChildren,color,copiedColor, plus standardButtonprops (size,radius,variant, etc.).2.
CustomCopyButtonProvides full flexibility to define custom copy button behavior and styling using JavaScript.
Built with the functions as props pattern.
CopyButton Examples
CustomCopyButton Example
This example uses an ActionIcon and a Tooltip as the copy button
defined in a .js file in /assets: