1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Js;
6:
7: use Atk4\Core\DiContainerTrait;
8:
9: /**
10: * Create Fomantic-UI Toast using JS.
11: *
12: * Example output: $('body').toast({options}).
13: */
14: class JsToast implements JsExpressionable
15: {
16: use DiContainerTrait;
17:
18: /** @var array<string, mixed> Various setting options as per Fomantic-UI toast module. */
19: public array $settings = [];
20:
21: /** @var string default CSS class for toast */
22: public $defaultCss = 'success';
23:
24: /**
25: * @param array<string, mixed>|string $settings
26: */
27: public function __construct($settings = null)
28: {
29: if (is_array($settings)) {
30: $this->settings = $settings;
31: } elseif (is_string($settings)) {
32: $this->settings['message'] = $settings;
33: }
34:
35: // set default CSS class
36: if (!array_key_exists('class', $this->settings)) {
37: $this->settings['class'] = $this->defaultCss;
38: }
39: }
40:
41: /**
42: * Set message to display in Toast.
43: *
44: * @param string $msg
45: *
46: * @return $this
47: */
48: public function setMessage($msg): self
49: {
50: $this->settings['message'] = $msg;
51:
52: return $this;
53: }
54:
55: #[\Override]
56: public function jsRender(): string
57: {
58: return (new Jquery('body'))->toast($this->settings)->jsRender();
59: }
60: }
61: