|
| 1 | +/* eslint-disable react/react-in-jsx-scope */ |
| 2 | +import { Sandpack } from '@codesandbox/sandpack-react'; |
| 3 | +import { githubLight } from '@codesandbox/sandpack-themes'; |
| 4 | +import dedent from 'dedent'; |
| 5 | + |
| 6 | +const settings = { |
| 7 | + js: { |
| 8 | + html: /* HTML */ ` |
| 9 | + <style> |
| 10 | + body { |
| 11 | + font-family: sans-serif; |
| 12 | + } |
| 13 | + </style> |
| 14 | + <div id="custom"></div> |
| 15 | + <div id="searchbox"></div> |
| 16 | + <div id="hits"></div> |
| 17 | + `, |
| 18 | + preamble: /* JS */ ` |
| 19 | + import 'instantsearch.css/themes/satellite-min.css'; |
| 20 | + import algoliasearch from 'algoliasearch/lite'; |
| 21 | + import instantsearch from 'instantsearch.js'; |
| 22 | + import { history } from 'instantsearch.js/es/lib/routers'; |
| 23 | + import { searchBox, hits } from 'instantsearch.js/es/widgets'; |
| 24 | + import { createWidgets } from './widget.ts'; |
| 25 | +
|
| 26 | + const search = instantsearch({ |
| 27 | + indexName: 'instant_search', |
| 28 | + searchClient: algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76'), |
| 29 | + future: { |
| 30 | + preserveSharedStateOnUnmount: true, |
| 31 | + }, |
| 32 | + routing: { |
| 33 | + router: history({ |
| 34 | + cleanUrlOnDispose: false, |
| 35 | + }) |
| 36 | + } |
| 37 | + }); |
| 38 | +
|
| 39 | + search.addWidgets([ |
| 40 | + ...createWidgets(document.querySelector('#custom')), |
| 41 | + searchBox({ |
| 42 | + container: '#searchbox', |
| 43 | + }), |
| 44 | + hits({ |
| 45 | + container: '#hits', |
| 46 | + templates: { |
| 47 | + item: (hit, { components }) => components.Highlight({ attribute: 'name', hit }), |
| 48 | + }, |
| 49 | + }), |
| 50 | + ]); |
| 51 | +
|
| 52 | + search.start(); |
| 53 | + `, |
| 54 | + dependencies: { |
| 55 | + // TODO: use current version somehow, both locally and in the built website |
| 56 | + 'instantsearch.js': 'latest', |
| 57 | + 'instantsearch.css': 'latest', |
| 58 | + algoliasearch: 'latest', |
| 59 | + }, |
| 60 | + filename: '/widget.ts', |
| 61 | + }, |
| 62 | + react: { |
| 63 | + html: /* HTML */ ` |
| 64 | + <style> |
| 65 | + body { |
| 66 | + font-family: sans-serif; |
| 67 | + } |
| 68 | + </style> |
| 69 | + <main id="root"></main> |
| 70 | + `, |
| 71 | + preamble: /* TSX */ ` |
| 72 | + import 'instantsearch.css/themes/satellite-min.css'; |
| 73 | + import React from "react"; |
| 74 | + import { createRoot } from "react-dom/client"; |
| 75 | + import algoliasearch from "algoliasearch/lite"; |
| 76 | + import { history } from "instantsearch.js/es/lib/routers"; |
| 77 | + import { InstantSearch, SearchBox, Hits, Highlight } from "react-instantsearch"; |
| 78 | + import { widgets } from "./widget.tsx"; |
| 79 | +
|
| 80 | + createRoot(document.getElementById('root')).render( |
| 81 | + <InstantSearch |
| 82 | + indexName="instant_search" |
| 83 | + searchClient={algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76')} |
| 84 | + future={{ |
| 85 | + preserveSharedStateOnUnmount: true, |
| 86 | + }} |
| 87 | + routing={{ |
| 88 | + router: history({ |
| 89 | + cleanUrlOnDispose: false, |
| 90 | + }) |
| 91 | + }} |
| 92 | + > |
| 93 | + {widgets} |
| 94 | + <SearchBox /> |
| 95 | + <Hits hitComponent={Hit}/> |
| 96 | + </InstantSearch> |
| 97 | + ); |
| 98 | +
|
| 99 | + function Hit({ hit }) { |
| 100 | + return <Highlight hit={hit} attribute="name" />; |
| 101 | + } |
| 102 | + `, |
| 103 | + dependencies: { |
| 104 | + react: 'latest', |
| 105 | + 'react-dom': 'latest', |
| 106 | + algoliasearch: 'latest', |
| 107 | + 'instantsearch.css': 'latest', |
| 108 | + 'react-instantsearch': 'latest', |
| 109 | + }, |
| 110 | + filename: '/widget.tsx', |
| 111 | + }, |
| 112 | + vue: { |
| 113 | + html: /* HTML */ ` |
| 114 | + <style> |
| 115 | + body { |
| 116 | + font-family: sans-serif; |
| 117 | + } |
| 118 | + </style> |
| 119 | + <main id="app"></main> |
| 120 | + `, |
| 121 | + preamble: ` |
| 122 | + import "instantsearch.css/themes/satellite-min.css"; |
| 123 | + import Vue from "vue"; |
| 124 | + import algoliasearch from "algoliasearch/lite"; |
| 125 | + import { history } from "instantsearch.js/es/lib/routers"; |
| 126 | + import { AisInstantSearch, AisHits, AisSearchBox } from "vue-instantsearch/vue2/es"; |
| 127 | + import Widget from "./Widget.vue"; |
| 128 | +
|
| 129 | + Vue.config.productionTip = false; |
| 130 | +
|
| 131 | + new Vue({ |
| 132 | + render: (h) => |
| 133 | + h( |
| 134 | + AisInstantSearch, |
| 135 | + { |
| 136 | + props: { |
| 137 | + searchClient: algoliasearch( |
| 138 | + "latency", |
| 139 | + "6be0576ff61c053d5f9a3225e2a90f76" |
| 140 | + ), |
| 141 | + indexName: "instant_search", |
| 142 | + future: { |
| 143 | + preserveSharedStateOnUnmount: true, |
| 144 | + }, |
| 145 | + routing: { |
| 146 | + router: history({ |
| 147 | + cleanUrlOnDispose: false, |
| 148 | + }) |
| 149 | + } |
| 150 | + }, |
| 151 | + }, |
| 152 | + [h(Widget), h(AisSearchBox), h(AisHits)] |
| 153 | + ), |
| 154 | + }).$mount("#app"); |
| 155 | + `, |
| 156 | + dependencies: { |
| 157 | + vue: '2', |
| 158 | + algoliasearch: 'latest', |
| 159 | + 'instantsearch.css': 'latest', |
| 160 | + 'vue-instantsearch': 'latest', |
| 161 | + }, |
| 162 | + filename: '/Widget.vue', |
| 163 | + }, |
| 164 | +}; |
| 165 | + |
| 166 | +export default function Sandbox({ |
| 167 | + code, |
| 168 | + flavor, |
| 169 | +}: { |
| 170 | + code: string; |
| 171 | + flavor: 'react' | 'js' | 'vue'; |
| 172 | +}) { |
| 173 | + const { preamble, html, filename, dependencies } = settings[flavor]; |
| 174 | + return ( |
| 175 | + <Sandpack |
| 176 | + files={{ |
| 177 | + '/index.html': { |
| 178 | + hidden: true, |
| 179 | + code: dedent(html), |
| 180 | + }, |
| 181 | + '/index.js': { |
| 182 | + code: dedent(preamble), |
| 183 | + }, |
| 184 | + [filename]: { |
| 185 | + code, |
| 186 | + }, |
| 187 | + }} |
| 188 | + customSetup={{ |
| 189 | + dependencies, |
| 190 | + entry: '/index.js', |
| 191 | + }} |
| 192 | + options={{ |
| 193 | + activeFile: filename, |
| 194 | + showNavigator: true, |
| 195 | + }} |
| 196 | + theme={githubLight} |
| 197 | + /> |
| 198 | + ); |
| 199 | +} |
0 commit comments