I think the solution is clearer, and already hinted at in your attributes
- Add a new input for the text, and set its value to the
btnText
attributes value, allow it to update that attribute on change - when you set the URL, don’t use
(post && post.title) || 'Click here'
instead use(post && post.title) || props.attributes.btnText
so that it falls back to the existing value - give the attribute a default value of
Click Here
when you register the block so that it starts out with that value
This way you could use a TextControl
, or even a richtext to allow italic and bold etc. Then you can use a toolbar button to trigger the link the same way buttons and hyperlinks do.
Note though that if you just want a button that links somewhere, you can use a core/button
block or core/buttons
. This would save time, allow some limited user control, and improve theme support by letting global styles control how the buttons look from theme.json
.
If you just want a button that looks different, you can also register a block style or block variant to allow users to pick alternative stylings