|
| 1 | +import { component$, useContext, useSignal, useTask$ } from '@builder.io/qwik'; |
| 2 | +import { |
| 3 | + SfButton, |
| 4 | + SfIconClose, |
| 5 | + SfIconLock, |
| 6 | + SfIconSearch, |
| 7 | + SfModal, |
| 8 | +} from 'qwik-storefront-ui'; |
| 9 | +import { ComponentExample } from '../../../components/utils/ComponentExample'; |
| 10 | +import { createControlsOptions } from '../../../components/utils/ControlsOptions'; |
| 11 | +import { ControlsType } from '../../../components/utils/types'; |
| 12 | +import { EXAMPLES_STATE } from '../layout'; |
| 13 | + |
| 14 | +const prefixSlotOptions = createControlsOptions({ |
| 15 | + none: undefined, |
| 16 | + 'Search icon': <SfIconSearch />, |
| 17 | +}); |
| 18 | +const suffixSlotOptions = createControlsOptions({ |
| 19 | + none: undefined, |
| 20 | + 'Lock icon': <SfIconLock />, |
| 21 | +}); |
| 22 | + |
| 23 | +export default component$(() => { |
| 24 | + const selectPrefix = useSignal<boolean>(); |
| 25 | + const selectSuffix = useSignal<boolean>(); |
| 26 | + |
| 27 | + const examplesState = useContext(EXAMPLES_STATE); |
| 28 | + |
| 29 | + useTask$(() => { |
| 30 | + examplesState.data = { |
| 31 | + controls: [ |
| 32 | + { |
| 33 | + type: 'text', |
| 34 | + modelName: 'slot', |
| 35 | + description: |
| 36 | + 'Only for demonstration purposes. Default slot, replaces example modal content', |
| 37 | + }, |
| 38 | + { |
| 39 | + type: 'boolean', |
| 40 | + modelName: 'open', |
| 41 | + propType: 'boolean', |
| 42 | + isRequired: true, |
| 43 | + description: 'If true modal is visible', |
| 44 | + }, |
| 45 | + { |
| 46 | + type: 'boolean', |
| 47 | + modelName: 'disableClickAway', |
| 48 | + propType: 'boolean', |
| 49 | + isRequired: true, |
| 50 | + description: |
| 51 | + "If true can't close modal when clicking outside of modal", |
| 52 | + }, |
| 53 | + { |
| 54 | + type: 'boolean', |
| 55 | + modelName: 'disableEsc', |
| 56 | + propType: 'boolean', |
| 57 | + isRequired: true, |
| 58 | + description: |
| 59 | + "If true can't close modal drawer when using ESC keyboard button", |
| 60 | + }, |
| 61 | + ] satisfies ControlsType, |
| 62 | + state: { |
| 63 | + slot: 'Hello', |
| 64 | + open: false, |
| 65 | + disableClickAway: false, |
| 66 | + disableEsc: false, |
| 67 | + }, |
| 68 | + }; |
| 69 | + }); |
| 70 | + |
| 71 | + useTask$(({ track }) => { |
| 72 | + track(() => examplesState.data.state); |
| 73 | + if (selectPrefix.value === null) return; |
| 74 | + selectPrefix.value = prefixSlotOptions.getValue( |
| 75 | + examplesState.data.state.slotPrefix |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + useTask$(({ track }) => { |
| 80 | + track(() => examplesState.data.state); |
| 81 | + if (selectSuffix.value === null) return; |
| 82 | + selectSuffix.value = suffixSlotOptions.getValue( |
| 83 | + examplesState.data.state.slotSuffix |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + return ( |
| 88 | + <ComponentExample componentContainerClass="space-x-2"> |
| 89 | + <SfButton |
| 90 | + type="button" |
| 91 | + preventdefault:click |
| 92 | + onClick$={() => { |
| 93 | + examplesState.data.state.open = true; |
| 94 | + console.log('value from button:', examplesState.data.state.open); |
| 95 | + }} |
| 96 | + > |
| 97 | + To Checkout |
| 98 | + </SfButton> |
| 99 | + |
| 100 | + {examplesState.data.state.open && ( |
| 101 | + <SfModal |
| 102 | + open={examplesState.data.state.open} |
| 103 | + disableClickAway={examplesState.data.state.disableClickAway} |
| 104 | + disableEsc={examplesState.data.state.disableEsc} |
| 105 | + onClose$={() => { |
| 106 | + examplesState.data.state.open = false; |
| 107 | + }} |
| 108 | + class="max-w-[90%] md:max-w-lg" |
| 109 | + as="section" |
| 110 | + role="alertdialog" |
| 111 | + aria-labelledby="promoModalTitle" |
| 112 | + aria-describedby="promoModalDesc" |
| 113 | + > |
| 114 | + <header> |
| 115 | + <SfButton |
| 116 | + square |
| 117 | + variant="tertiary" |
| 118 | + class="absolute right-2 top-2" |
| 119 | + onClick$={() => { |
| 120 | + examplesState.data.state.open = false; |
| 121 | + }} |
| 122 | + > |
| 123 | + <SfIconClose /> |
| 124 | + </SfButton> |
| 125 | + <h3 |
| 126 | + id="promoModalTitle" |
| 127 | + class="font-bold typography-headline-4 md:typography-headline-3" |
| 128 | + > |
| 129 | + You might miss out on great deals |
| 130 | + </h3> |
| 131 | + </header> |
| 132 | + <p id="promoModalDesc" class="mt-2"> |
| 133 | + There are special offers for some of the items on your wishlist. Do |
| 134 | + you want to see these deals before proceeding to checkout page? |
| 135 | + </p> |
| 136 | + <footer class="flex justify-end gap-4 mt-4"> |
| 137 | + <SfButton |
| 138 | + type="button" |
| 139 | + variant="secondary" |
| 140 | + onClick$={() => { |
| 141 | + examplesState.data.state.open = false; |
| 142 | + }} |
| 143 | + > |
| 144 | + Skip |
| 145 | + </SfButton> |
| 146 | + <SfButton |
| 147 | + type="button" |
| 148 | + onClick$={() => { |
| 149 | + examplesState.data.state.open = false; |
| 150 | + }} |
| 151 | + > |
| 152 | + Yes! |
| 153 | + </SfButton> |
| 154 | + </footer> |
| 155 | + </SfModal> |
| 156 | + )} |
| 157 | + </ComponentExample> |
| 158 | + ); |
| 159 | +}); |
0 commit comments