UNPKG

6.99 kBMarkdownView Raw
1<p align="center"><img width="45%" src="https://cdn.rawgit.com/krakenjs/zoid/master/zoid.png"></p>
2
3----
4
5A cross-domain component toolkit, supporting:
6
7- Render an iframe or popup on a different domain, and pass down props, including objects and functions
8- Call callbacks natively from the child window without worrying about post-messaging or cross-domain restrictions
9- Create and expose components to share functionality from your site to others!
10- Render your component directly as a React, Vue or Angular component!
11
12It's 'data-down, actions up' style components, but 100% cross-domain using iframes and popups!
13
14-----
15
16### [API Docs](./docs/api.md)
17
18Public options and methods supported by zoid
19
20### [Demos](http://krakenjs.com/zoid/demo/index.htm)
21
22Working demos of different zoid patterns
23
24### [Demo App](https://github.com/krakenjs/zoid-demo)
25
26Forkable demo app with build, test, publishing and demos pre-configured.
27
28### [Example](./docs/example.md)
29
30A full example of a cross-domain component using zoid
31
32-----
33
34### Quick example
35
36Define a component to be put on both the parent and child pages:
37
38```javascript
39var MyLoginComponent = zoid.create({
40
41 tag: 'my-login-component',
42 url: 'http://www.my-site.com/my-login-component'
43});
44```
45
46Render the component on the parent page:
47
48```javascript
49<div id="container"></div>
50
51<script src="script-where-my-login-component-is-defined.js"></script>
52<script>
53 MyLoginComponent({
54
55 prefilledEmail: 'foo@bar.com',
56
57 onLogin: function(email) {
58 console.log('User logged in with email:', email);
59 }
60
61 }).render('#container');
62</script>
63```
64
65Implement the component in the iframe:
66
67```javascript
68<input type="text" id="email" />
69<input type="password" id="password" />
70<button id="login">Log In</button>
71
72<script src="script-where-my-login-component-is-defined.js"></script>
73<script>
74 var email = document.querySelector('#email');
75 var password = document.querySelector('#password');
76 var button = document.querySelector('#login');
77
78 email.value = window.xprops.prefilledEmail;
79
80 function validUser (email, password) {
81 return email && password;
82 }
83
84 button.addEventListener('click', function() {
85 if (validUser(email.value, password.value)) {
86 window.xprops.onLogin(email.value);
87 }
88 });
89</script>
90```
91
92### Useful Links
93
94- [Introducing zoid](https://medium.com/@bluepnume/introducing-zoid-seamless-cross-domain-web-components-from-paypal-c0144f3e82bf#.ikbg9r1ml)
95- [Turn your web-app into a cross-domain component with five lines of code](https://medium.com/@bluepnume/turn-your-web-app-into-a-cross-domain-component-with-5-lines-of-code-ced01e6795f9#.w8ea7h6ky)
96- [A full example of how to implement and use a zoid](./docs/example.md)
97- [Building PayPal's Button with zoid](https://medium.com/@bluepnume/less-is-more-reducing-thousands-of-paypal-buttons-into-a-single-iframe-using-zoid-d902d71d8875#.o3ib7y58n)
98- [PayPal Checkout - zoid powered Button and Checkout components](https://github.com/paypal/paypal-checkout)
99- [Post-Robot - the cross-domain messaging library which powers zoid](https://github.com/krakenjs/post-robot)
100
101#### Framework Specific
102
103- [Build a cross-domain React component](https://medium.com/@bluepnume/creating-a-cross-domain-react-component-with-zoid-fbcccc4778fd#.73jnwv44c)
104- [Introducing support for cross-domain Glimmer components, with zoid](https://medium.com/@bluepnume/introducing-support-for-cross-domain-glimmer-components-with-zoid-21287c9f91f1)
105
106## Rationale
107
108
109**Writing cross domain components is tricky.**
110
111Consider this: I own `foo.com`, you own `bar.com`, and I have some functionality I want to share on your page.
112I could just give you some javascript to load in your page. But then:
113
114- What if I've written a component in React, but you're using some other framework?
115- What if I have secure form fields, or secure data I don't want your site to spy on?
116- What if I need to make secure calls to my back-end, without resorting to CORS?
117
118
119**What about an iframe?**
120
121You could just use a vanilla iframe for all of this. But:
122
123- You have to pass data down in the url, or with a post-message.
124- You need to set up post-message listeners to get events back up from the child.
125- You need to deal with error cases, like if your iframe fails to load or doesn't respond to a post-message.
126- You need to think carefully about how to expose all this functionality behind a simple, clear interface.
127
128
129**zoid solves all of these problems.**
130
131- You pass data and callbacks down as a javascript object
132- zoid renders the component and passes down the data
133- The child calls your callbacks when it's ready
134
135It will even automatically generate React and Angular bindings, so people can drop-in your component anywhere and not
136worry about iframes or post-messages.
137
138
139## FAQ
140
141- **Do I need to use a particular framework like React to use zoid?**
142
143 No, zoid is framework agnostic. You can:
144
145 - Use it with vanilla javascript.
146 - Use it with any framework of your choice.
147 - Use it with React or Angular and take advantage of the automatic bindings on the parent page
148
149- **Why write another ui / component library?**
150
151 This isn't designed to replace libraries like React, which are responsible for rendering same-domain components. In fact, the only
152 real rendering zoid does is iframes and popups; the rest is up to you! You can build your components using any framework,
153 library or pattern you want, then use zoid to expose your components cross-domain. It should play nicely with any other framework!
154
155- **Aren't iframes really slow?**
156
157 Yes, but there are a few things to bear in mind here:
158
159 - zoid isn't designed for building components for your own site. For that you should use native component libraries
160 like React, which render quickly onto your page. Use zoid to share functionality with other sites, that you can't
161 share native-javascript components with
162
163 - zoid also provides mechanisms for pre-rendering html and css into iframes and popups, so you can at least render a
164 loading spinner, or maybe something more advanced, while the new window loads its content.
165
166- **I don't want to bother with popups, can I get zoid with just the iframe support?**
167
168 You can indeed. There's an `zoid.frame.js` and `zoid.frame.min.js` in the `dist/` folder. There's a lot of
169 magic that's needed to make popups work with IE, and that's all trimmed out.
170
171- **Can I contribute?**
172
173 By all means! But please raise an issue first if it's more than a small change, to discuss the feasibility.
174
175- **Is this different to [react-frame-component](https://github.com/ryanseddon/react-frame-component)?**
176
177 Yes. `react-frame-component` allows you to render html into a sandboxed iframe on the same domain. `zoid` is geared
178 around sharing functionality from one domain to another, in a cross-domain iframe.
179
180## Browser Support
181
182- Internet Explorer 9+
183- Chrome 27+
184- Firefox 30+
185- Safari 5.1+
186- Opera 23+