|
| 1 | +--- |
| 2 | +title: Custom Modal API |
| 3 | +--- |
| 4 | + |
| 5 | +# Custom Modal API |
| 6 | +:::warning |
| 7 | +The only intended use of this API is within the editor, do not make blocks to display these custom modals. |
| 8 | +::: |
| 9 | + |
| 10 | +This API allows you to create ScratchBlocks modals meant to contain custom content. |
| 11 | + |
| 12 | +To use this API, you need a reference to ScratchBlocks/Blockly: |
| 13 | + |
| 14 | +```js |
| 15 | +/* get ScratchBlocks if availiable... */ |
| 16 | +if (Scratch.gui) { |
| 17 | + Scratch.gui.getBlockly().then(ScratchBlocks => { |
| 18 | + // ScratchBlocks can be used here safely |
| 19 | + }); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## Creation |
| 24 | + |
| 25 | +Here is a quick snippet of a `customPrompt` modal: |
| 26 | + |
| 27 | +```js |
| 28 | +const modal = await ScratchBlocks.customPrompt({ |
| 29 | + title: "title", |
| 30 | +}, { |
| 31 | + content: { width: "500px" } |
| 32 | +}, [ |
| 33 | + { name: "OK", role: "ok", callback: () => console.log("Confirmed") }, |
| 34 | + { name: "Cancel", role: "close", callback: () => console.log("Cancelled") } |
| 35 | +]); |
| 36 | +``` |
| 37 | + |
| 38 | +### Modal configuration |
| 39 | + |
| 40 | +The first parameter of `customPrompt` determines the configuration of the modal. |
| 41 | + |
| 42 | +```js |
| 43 | +{ |
| 44 | + title: string, |
| 45 | + scrollable: boolean?, |
| 46 | +} |
| 47 | +``` |
| 48 | +
|
| 49 | +The only required property here is the `title` property: |
| 50 | +
|
| 51 | +<img src="/img/docimages/custommodal_title.png" alt="Image showing modal title"></img> |
| 52 | +
|
| 53 | +It's recommended to set `scrollable` to `true` if your modal has resizable elements or can get really tall on the screen. |
| 54 | +
|
| 55 | +### Modal styling |
| 56 | +
|
| 57 | +The second parameter determines the style of the modal on the page. This parameter is optional. |
| 58 | +
|
| 59 | +```js |
| 60 | +{ |
| 61 | + content: CSSStyleDeclaration?, |
| 62 | + overlay: CSSStyleDeclaration?, |
| 63 | +} |
| 64 | +``` |
| 65 | +
|
| 66 | +:::note |
| 67 | +Both parts of the modal are a `div`. The `content` part of the modal is contained within the `overlay` div. Note that the entire modal contents (including header and buttons) are contained within the `content` div. If you want to style more parts of the modal, use the `parentNode`(s) of the element you are given after making a custom prompt. |
| 68 | +::: |
| 69 | +
|
| 70 | +If you want your modal to have a width of `500px` you would set this in the `content` styles: |
| 71 | +
|
| 72 | +```js |
| 73 | +{ |
| 74 | + content: { width: "500px" }, |
| 75 | +} |
| 76 | +``` |
| 77 | +<img src="/img/docimages/custommodal_width.png" alt="Image showing modal width"></img> |
| 78 | +
|
| 79 | +:::warning |
| 80 | +Note that using certain CSS styles like `height` and making them bigger than the content of the modal can cause the modal's outline to be larger than the modal. |
| 81 | +You probably don't need to use a value other than `"auto"` for `height`, so it's best to either use that value or leave it out of the styles. |
| 82 | +
|
| 83 | +<img src="/img/docimages/custommodal_height_warning.png" height="256px" alt="Image showing modal height error"></img> |
| 84 | +::: |
| 85 | +
|
| 86 | +If you want your modal to have a transparent red background, you would set this in the `overlay` styles: |
| 87 | +
|
| 88 | +```js |
| 89 | +{ |
| 90 | + overlay: { background: "rgba(255, 0, 0, 0.5)" }, |
| 91 | +} |
| 92 | +``` |
| 93 | +<img src="/img/docimages/custommodal_redbackground.png" alt="Image showing modal with red background"></img> |
| 94 | +
|
| 95 | +You can have both `content` and `overlay` styles at the same time. |
| 96 | +
|
| 97 | +### Modal buttons |
| 98 | +
|
| 99 | +The third parameter determines the buttons present on the modal. This parameter is optional. |
| 100 | +
|
| 101 | +This is an array of objects, with each object representing a button and some information + behavior. |
| 102 | +
|
| 103 | +Each button appears right-to-left in the modal. It's recommended to define your OK button first, then your cancel button elsewhere. |
| 104 | +Defining buttons in that order will match with other Scratch modals. |
| 105 | +
|
| 106 | +```js |
| 107 | +[ |
| 108 | + { |
| 109 | + name: string, |
| 110 | + role: "ok"|"close"|null, |
| 111 | + class: "ok"|"cancel"|null, |
| 112 | + style: CSSStyleDeclaration?, |
| 113 | + dontClose: boolean, |
| 114 | + callback: function(): void |
| 115 | + } |
| 116 | +] |
| 117 | +``` |
| 118 | +
|
| 119 | +The `role` property defines how the button should behave. Other interactions with the modal can trigger the button if it has a `role`. |
| 120 | +- The `"close"` role will run the callback when the <img src="https://penguinmod.com/dismiss.svg" height="18px" alt="X"></img> button on the modal is clicked. |
| 121 | +- The `"close"` role will run the callback when the modal is closed via Browser Back or the modal is closed by clicking outside of it. |
| 122 | +
|
| 123 | +Do not define buttons with the same `role` as another button, only use a `role` once. |
| 124 | +
|
| 125 | +The `class` property allows you to set predefined styles for the specified button. |
| 126 | +
|
| 127 | +The `style` property allows you to define custom CSS on the button. |
| 128 | +
|
| 129 | +The `dontClose` property will make the button not close the modal when clicked. |
| 130 | +
|
| 131 | +#### OK button |
| 132 | +Most modals that perform an action should have an OK button. To create this button, use the `"ok"` role when making a button. |
| 133 | +Here is a simple example: |
| 134 | +
|
| 135 | +```js |
| 136 | +[ |
| 137 | + { |
| 138 | + name: "OK", |
| 139 | + role: "ok", |
| 140 | + callback: () => console.log("Confirmed") |
| 141 | + } |
| 142 | +] |
| 143 | +``` |
| 144 | +<img src="/img/docimages/custommodal_ok.png" alt="Modal with OK button"></img> |
| 145 | +
|
| 146 | +:::note |
| 147 | +To use the OK button's styling on another button, set `class` to `"ok"`. |
| 148 | +::: |
| 149 | +
|
| 150 | +#### Cancel button |
| 151 | +If you want to have a literal cancel button on the modal, use the `"close"` role when making a button. The role name is different from `"cancel"` since this button is also used when the modal is *closed* by actions like pressing the <img src="https://penguinmod.com/dismiss.svg" height="18px" alt="X"></img> button. |
| 152 | +
|
| 153 | +```js |
| 154 | +[ |
| 155 | + { |
| 156 | + name: "Cancel", |
| 157 | + role: "close", |
| 158 | + callback: () => console.log("Cancelled") |
| 159 | + } |
| 160 | +] |
| 161 | +``` |
| 162 | +<img src="/img/docimages/custommodal_cancel.png" alt="Modal with Cancel button"></img> |
| 163 | +
|
| 164 | +:::note |
| 165 | +To use the Cancel button's styling on another button, set `class` to `"cancel"`. |
| 166 | +::: |
| 167 | +
|
| 168 | +## Adding elements |
| 169 | +
|
| 170 | +Now that you have your modal on the screen, you can add elements to it just by appending to the element returned: |
| 171 | +
|
| 172 | +```js |
| 173 | +const modal = await ScratchBlocks.customPrompt({ |
| 174 | + title: "title", |
| 175 | +}, { |
| 176 | + content: { width: "500px" } |
| 177 | +}, [ |
| 178 | + { name: "OK", role: "ok", callback: () => console.log("Confirmed") }, |
| 179 | + { name: "Cancel", role: "close", callback: () => console.log("Cancelled") } |
| 180 | +]); |
| 181 | + |
| 182 | +modal.appendChild(document.createElement("textarea")); |
| 183 | +``` |
| 184 | +
|
| 185 | +<img src="/img/docimages/custommodal_elements.png" alt="Modal with textarea element"></img> |
0 commit comments