Create a text component. Args: *children: The children of the component. can_copy: Whether a copy button should appears. copy_button: A custom copy button to override the default one. **props: The props to pass to the component. Retur
(
cls,
*children,
can_copy: Optional[bool] = False,
copy_button: Optional[Union[bool, Component]] = None,
**props,
)
| 430 | |
| 431 | @classmethod |
| 432 | def create( |
| 433 | cls, |
| 434 | *children, |
| 435 | can_copy: Optional[bool] = False, |
| 436 | copy_button: Optional[Union[bool, Component]] = None, |
| 437 | **props, |
| 438 | ): |
| 439 | """Create a text component. |
| 440 | |
| 441 | Args: |
| 442 | *children: The children of the component. |
| 443 | can_copy: Whether a copy button should appears. |
| 444 | copy_button: A custom copy button to override the default one. |
| 445 | **props: The props to pass to the component. |
| 446 | |
| 447 | Returns: |
| 448 | The text component. |
| 449 | """ |
| 450 | # This component handles style in a special prop. |
| 451 | custom_style = props.pop("custom_style", {}) |
| 452 | |
| 453 | if "theme" not in props: |
| 454 | # Default color scheme responds to global color mode. |
| 455 | props["theme"] = color_mode_cond(light="one-light", dark="one-dark") |
| 456 | |
| 457 | # react-syntax-highlighter doesnt have an explicit "light" or "dark" theme so we use one-light and one-dark |
| 458 | # themes respectively to ensure code compatibility. |
| 459 | if "theme" in props and not isinstance(props["theme"], Var): |
| 460 | props["theme"] = ( |
| 461 | "one-light" |
| 462 | if props["theme"] == "light" |
| 463 | else "one-dark" |
| 464 | if props["theme"] == "dark" |
| 465 | else props["theme"] |
| 466 | ) |
| 467 | |
| 468 | if can_copy: |
| 469 | code = children[0] |
| 470 | copy_button = ( # type: ignore |
| 471 | copy_button |
| 472 | if copy_button is not None |
| 473 | else Button.create( |
| 474 | Icon.create(tag="copy"), |
| 475 | on_click=set_clipboard(code), |
| 476 | style=Style({"position": "absolute", "top": "0.5em", "right": "0"}), |
| 477 | ) |
| 478 | ) |
| 479 | custom_style.update({"padding": "1em 3.2em 1em 1em"}) |
| 480 | else: |
| 481 | copy_button = None |
| 482 | |
| 483 | # Transfer style props to the custom style prop. |
| 484 | for key, value in props.items(): |
| 485 | if key not in cls.get_fields(): |
| 486 | custom_style[key] = value |
| 487 | |
| 488 | # Carry the children (code) via props |
| 489 | if children: |
no test coverage detected