@inditeai/react 0.0.2 → 0.0.3

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/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- // v0.0.2
1
+ // v0.0.3
2
2
  import{jsx as r}from"react/jsx-runtime";import{useRef as e,useEffect as t,useCallback as n}from"react";import"@inditeai/js/dist/web";export*from"@inditeai/js/dist";"function"==typeof SuppressedError&&SuppressedError;const o=n=>{var{style:o,className:c}=n,u=function(r,e){var t={};for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&e.indexOf(n)<0&&(t[n]=r[n]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(r);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(r,n[o])&&(t[n[o]]=r[n[o]])}return t}(n,["style","className"]);const l=e(null);return t((()=>{l.current&&Object.assign(l.current,u)}),[u]),r("bot-standard",{ref:l,style:o,class:c})},c=r=>{const o=e(null),c=n((r=>{const e=document.createElement("bot-bubble");o.current=e,u(o.current,r),document.body.prepend(o.current)}),[]);t((()=>{o.current||c(r),u(o.current,r)}),[c,r]),t((()=>()=>{var r;null===(r=o.current)||void 0===r||r.remove(),o.current=null}),[]);const u=(r,e)=>{Object.assign(r,e)};return null},u=o=>{const c=e(null),u=e(null),l=n((r=>{var e;const t=document.createElement("bot-popup");u.current=t,s(u.current,r),c.current?null===(e=c.current)||void 0===e||e.append(u.current):console.warn("Could not attach popup to container because containerRef.current is null")}),[]);t((()=>{u.current||l(o),s(u.current,o)}),[l,o]),t((()=>()=>{var r;null===(r=u.current)||void 0===r||r.remove(),u.current=null}),[]);const s=(r,e)=>{Object.assign(r,e)};return r("div",{ref:c})};export{c as Bubble,u as Popup,o as Standard};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inditeai/react",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Convenient library to display bots on your React app",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,6 +11,10 @@
11
11
  "lint": "eslint --fix \"src/**/*.ts*\"",
12
12
  "format:check": "prettier --check ./src"
13
13
  },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/Bacancy-DataProphets/Indite-packages.git"
17
+ },
14
18
  "license": "MIT",
15
19
  "dependencies": {
16
20
  "@ladle/react": "2.5.1"
package/src/Bubble.tsx ADDED
@@ -0,0 +1,51 @@
1
+ import React, { useCallback, useEffect, useRef } from 'react'
2
+ import type { BubbleProps } from '@inditeai/js/dist'
3
+ import '@inditeai/js/dist/web'
4
+
5
+ type Props = BubbleProps
6
+
7
+ declare global {
8
+ namespace JSX {
9
+ interface IntrinsicElements {
10
+ 'bot-bubble': React.DetailedHTMLProps<
11
+ React.HTMLAttributes<HTMLElement>,
12
+ HTMLElement
13
+ >
14
+ }
15
+ }
16
+ }
17
+
18
+ type BubbleElement = HTMLElement & Props
19
+
20
+ export const Bubble = (props: Props) => {
21
+ const bubbleElement = useRef<BubbleElement | null>(null)
22
+
23
+ const attachBubbleToDom = useCallback((props: Props) => {
24
+ const newBubbleElement = document.createElement(
25
+ 'bot-bubble'
26
+ ) as BubbleElement
27
+ bubbleElement.current = newBubbleElement
28
+ injectPropsToElement(bubbleElement.current, props)
29
+ document.body.prepend(bubbleElement.current)
30
+ }, [])
31
+
32
+ useEffect(() => {
33
+ if (!bubbleElement.current) attachBubbleToDom(props)
34
+ injectPropsToElement(bubbleElement.current as BubbleElement, props)
35
+ }, [attachBubbleToDom, props])
36
+
37
+ useEffect(() => {
38
+ return () => {
39
+ bubbleElement.current?.remove()
40
+ bubbleElement.current = null
41
+ }
42
+ }, [])
43
+
44
+ const injectPropsToElement = (element: BubbleElement, props: Props) => {
45
+ Object.assign(element, props)
46
+ }
47
+
48
+ return null
49
+ }
50
+
51
+ export default Bubble
package/src/Popup.tsx ADDED
@@ -0,0 +1,56 @@
1
+ import React, { useCallback, useEffect, useRef } from 'react'
2
+ import type { PopupProps } from '@inditeai/js/dist'
3
+ import '@inditeai/js/dist/web'
4
+
5
+ type Props = PopupProps
6
+
7
+ declare global {
8
+ namespace JSX {
9
+ interface IntrinsicElements {
10
+ 'bot-popup': React.DetailedHTMLProps<
11
+ React.HTMLAttributes<HTMLElement>,
12
+ HTMLElement
13
+ > & { class?: string }
14
+ }
15
+ }
16
+ }
17
+
18
+ type PopupElement = HTMLElement & Props
19
+
20
+ export const Popup = (props: Props) => {
21
+ const containerRef = useRef<HTMLDivElement | null>(null)
22
+ const popupRef = useRef<PopupElement | null>(null)
23
+
24
+ const attachPopupToContainer = useCallback((props: Props) => {
25
+ const popupElement = document.createElement('bot-popup') as PopupElement
26
+ popupRef.current = popupElement
27
+ injectPropsToElement(popupRef.current, props)
28
+ if (!containerRef.current) {
29
+ console.warn(
30
+ 'Could not attach popup to container because containerRef.current is null'
31
+ )
32
+ return
33
+ }
34
+ containerRef.current?.append(popupRef.current)
35
+ }, [])
36
+
37
+ useEffect(() => {
38
+ if (!popupRef.current) attachPopupToContainer(props)
39
+ injectPropsToElement(popupRef.current as PopupElement, props)
40
+ }, [attachPopupToContainer, props])
41
+
42
+ useEffect(() => {
43
+ return () => {
44
+ popupRef.current?.remove()
45
+ popupRef.current = null
46
+ }
47
+ }, [])
48
+
49
+ const injectPropsToElement = (element: PopupElement, props: Props) => {
50
+ Object.assign(element, props)
51
+ }
52
+
53
+ return <div ref={containerRef} />
54
+ }
55
+
56
+ export default Popup
@@ -0,0 +1,34 @@
1
+ import React, { useEffect, useRef } from 'react'
2
+ import type { BotProps } from '@inditeai/js/dist'
3
+ import '@inditeai/js/dist/web'
4
+
5
+ type Props = BotProps & {
6
+ style?: React.CSSProperties
7
+ className?: string
8
+ }
9
+
10
+ declare global {
11
+ namespace JSX {
12
+ interface IntrinsicElements {
13
+ 'bot-standard': React.DetailedHTMLProps<
14
+ React.HTMLAttributes<HTMLElement>,
15
+ HTMLElement
16
+ > & { class?: string }
17
+ }
18
+ }
19
+ }
20
+
21
+ type StandardElement = HTMLElement & Props
22
+
23
+ export const Standard = ({ style, className, ...assignableProps }: Props) => {
24
+ const ref = useRef<StandardElement | null>(null)
25
+
26
+ useEffect(() => {
27
+ if (!ref.current) return
28
+ Object.assign(ref.current, assignableProps)
29
+ }, [assignableProps])
30
+
31
+ return <bot-standard ref={ref} style={style} class={className} />
32
+ }
33
+
34
+ export default Standard
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { Standard } from './Standard'
2
+ export { Bubble } from './Bubble'
3
+ export { Popup } from './Popup'
4
+ export * from '@inditeai/js/dist'