playhtml 2.0.6 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
4
4
 
5
+ ## 2.0.7 - 2023-10-02
6
+
7
+ - upgrading y-partykit and yjs to latest for improved performance
8
+
5
9
  ## 2.0.5 - 2023-09-11
6
10
 
7
11
  - fixed an error with setting up elements before the provider was synced which lead to incorrect initial element states that didn't sync.
package/README.md CHANGED
@@ -14,6 +14,8 @@ If you enjoy this, please consider [sponsoring the project or sending a small do
14
14
 
15
15
  ## Usage
16
16
 
17
+ ### vanilla html / simple browser usage
18
+
17
19
  To use this library, you can import the library and the associated styles from a CDN (in this case we will use [unpkg.com](https://unpkg.com)). Please make sure to do this after all the HTML elements on the page and ensure that the HTML elements you are "magic-ifying" have an `id` set.
18
20
 
19
21
  ```html
@@ -51,6 +53,77 @@ If you have dynamic elements that are hydrated after the initial load, you can c
51
53
  </script>
52
54
  ```
53
55
 
56
+ ### react / next.js / other frameworks
57
+
58
+ **react**
59
+ `@playhtml/react` provides components out of the box corresponding to each of the capabilities. It manages all the state syncing for you, so you can reactively render your component based on whatever data is coming in.
60
+
61
+ Install by using your preferred package manager
62
+
63
+ ```bash
64
+ npm install @playhtml/react # or
65
+ # yarn add @playhtml/react
66
+ ```
67
+
68
+ The callback handlers are slightly different to match the reactive patterns of React. For example, instead of `updateElement`, which is a vanilla javascript recreation of a reactive state management system, you can simply use your own state hooks or call `setData` to modify the shared state.
69
+
70
+ You must import `playhtml` from the package and call `playhtml.init` somewhere before your components are rendered. This can be done anywhere in the code (unless you are using a framework with Server-Side Rendering, SSR, like with Next.JS, see below for how to handle this).
71
+
72
+ ```tsx
73
+ // candle
74
+ playhtml.init();
75
+ export function Candle() {
76
+ return (
77
+ <CanPlayElement
78
+ defaultData={{ on: false }}
79
+ onClick={(_e, { data, setData }) => {
80
+ setData({ on: !data.on });
81
+ }}
82
+ >
83
+ {({ data }) => (
84
+ <img
85
+ src={data.on ? "/candle-gif.gif" : "/candle-off.png"}
86
+ selector-id=".candle"
87
+ className="candle"
88
+ />
89
+ )}
90
+ </CanPlayElement>
91
+ );
92
+ }
93
+ ```
94
+
95
+ Refer to `packages/react/example.tsx` for a full list of examples.
96
+
97
+ **next.js**
98
+ Handling Next is more difficult due to the browser-first nature of `playhtml` and how you have to handle the same code running server-side with Next.
99
+
100
+ To initialize `playhtml`, you'll need to dynamically import `playhtml` and call init in a `useEffect` in the top-level component.
101
+
102
+ ```tsx
103
+ async function initPlayhtml() {
104
+ const playhtml = (await import("@playhtml/react")).playhtml;
105
+
106
+ playhtml.init();
107
+ }
108
+ // ... my component
109
+ useEffect(() => {
110
+ void initPlayhtml();
111
+ });
112
+ ```
113
+
114
+ Then, in your components, you'll need to turn off `ssr` for dynamically importing the `playhtml` elements.
115
+
116
+ ```tsx
117
+ import type { CanPlayElement as CanPlayElementType } from "@playhtml/react";
118
+ import dynamic from "next/dynamic";
119
+ const CanPlayElement = dynamic(
120
+ () => import("@playhtml/react").then((c) => c.CanPlayElement),
121
+ { ssr: false }
122
+ ) as typeof CanPlayElementType;
123
+ ```
124
+
125
+ ---
126
+
54
127
  To create your own custom element, refer to the [can-play](#can-play) section.
55
128
 
56
129
  If you're trying this out and having trouble, please message me ([email](mailto:spencerc99@gmail.com), [twitter](https://twitter.com/spencerc99)) and I'm happy to help out!
@@ -123,6 +196,7 @@ IDs are recommended on all elements to uniquely identify them. If you are applyi
123
196
  These capabilities are common ones that have been designed and created by the community. You should expect that they are relatively well-tested, and they simply build on top of the same API and constructs that `can-play` uses.
124
197
 
125
198
  ### `can-move`
199
+
126
200
  https://github.com/spencerc99/playhtml/assets/14796580/215c7631-d71f-40e6-bdda-bd8146a88006
127
201
 
128
202
  Creates a movable element using 2D `translate` on the element. Dragging the element around will move it
@@ -133,6 +207,7 @@ Creates a movable element using 2D `translate` on the element. Dragging the elem
133
207
  - This currently doesn't work on touch screens.
134
208
 
135
209
  ### `can-toggle`
210
+
136
211
  https://github.com/spencerc99/playhtml/assets/14796580/7e667c06-c32e-4369-b250-c9ca321de163
137
212
 
138
213
  _from https://twitter.com/spencerc99/status/1681048824884895744_
@@ -140,6 +215,7 @@ _from https://twitter.com/spencerc99/status/1681048824884895744_
140
215
  Creates an element that can be switched on and off. Clicking the element will toggle the `clicked` class on the element.
141
216
 
142
217
  ### `can-duplicate`
218
+
143
219
  https://github.com/spencerc99/playhtml/assets/14796580/6d9f1228-08cf-45a7-987a-8bebfaaefd83
144
220
 
145
221
  _used to programmatically and dynamically create new playhtml elements that aren't included in the initial HTML_
package/dist/main.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  /// <reference lib="dom" />
2
2
 
3
- import { ElementAwarenessEventHandlerData } from '../../common/src';
4
- import { ElementData } from '../../common/src';
5
- import { ElementEventHandlerData } from '../../common/src';
6
- import { ElementSetupData } from '../../common/src';
7
- import { ModifierKey } from '../../common/src';
3
+ import { ElementAwarenessEventHandlerData } from '@playhtml/common';
4
+ import { ElementData } from '@playhtml/common';
5
+ import { ElementEventHandlerData } from '@playhtml/common';
6
+ import { ElementSetupData } from '@playhtml/common';
7
+ import { ModifierKey } from '@playhtml/common';
8
8
  import { TagType } from '@playhtml/common';
9
9
  import * as Y from 'yjs';
10
10
  import YPartyKitProvider from 'y-partykit/provider';