UNPKG

@highloop/feedback-react

Version:

147 lines (139 loc) 4.63 kB
import { createPopoverWidget, createInlineWidget, createDialogWidget } from '@highloop/feedback'; export * from '@highloop/feedback'; import React, { useRef, useEffect, useMemo } from 'react'; var FeedbackButton = function FeedbackButton(props) { var ref = useRef(); var instanceRef = useRef(); useEffect(function () { if (!ref.current || typeof window == 'undefined') return; createPopoverWidget(props.id, ref.current, { theme: props.theme, text: props.text, meta: props.meta, position: props.position, closeOnOutsideClick: props.closeOnOutsideClick, expanded: props.expanded }).then(function (instance) { instanceRef.current = instance; })["catch"](function (err) { return console.warn('[highloop-feedback]: Could not create widget', err); }); return function () { if (!instanceRef.current) return; instanceRef.current.destroy(); }; }, [props.id]); useEffect(function () { if (!instanceRef.current) return; instanceRef.current.setTheme(props.theme); }, [props.theme]); useEffect(function () { if (!instanceRef.current) return; instanceRef.current.setMeta(props.meta); }, [props.meta]); var buttonProps = useMemo(function () { var buttonProps = Object.assign({ children: 'Feedback', style: !props.className && !props.style ? { color: 'white', background: 'black', height: 34, border: 'none', borderRadius: 8, padding: '0px 14px', fontSize: 14, fontWeight: 500 } : {} }, props); delete buttonProps.id; delete buttonProps.resetOnSubmit; delete buttonProps.theme; delete buttonProps.text; delete buttonProps.meta; delete buttonProps.position; delete buttonProps.expanded; delete buttonProps.closeOnOutsideClick; return buttonProps; }, [props]); return React.createElement("button", Object.assign({}, buttonProps, { ref: ref })); }; var FeedbackWidget = function FeedbackWidget(_ref) { var id = _ref.id, resetOnSubmit = _ref.resetOnSubmit, theme = _ref.theme, text = _ref.text, meta = _ref.meta, expanded = _ref.expanded; var ref = useRef(); var instanceRef = useRef(); useEffect(function () { if (!ref.current || typeof window == 'undefined') return; createInlineWidget(id, ref.current, { meta: meta, resetOnSubmit: resetOnSubmit, theme: theme, text: text, expanded: expanded }).then(function (instance) { instanceRef.current = instance; })["catch"](function (err) { return console.warn('[highloop-feedback]: Could not create widget', err); }); return function () { if (!instanceRef.current) return; instanceRef.current.destroy(); }; }, [id]); useEffect(function () { if (!instanceRef.current) return; instanceRef.current.setTheme(theme); }, [theme]); useEffect(function () { if (!instanceRef.current) return; instanceRef.current.setMeta(meta); }, [meta]); return React.createElement("div", { ref: ref }); }; var useFeedbackDialog = function useFeedbackDialog(id, opts) { var instanceRef = useRef(); useEffect(function () { createDialogWidget(id, opts).then(function (instance) { instanceRef.current = instance; })["catch"](function (err) { return console.warn('[highloop-feedback]: Could not create widget', err); }); return function () { if (!instanceRef.current) return; instanceRef.current.destroy(); }; }, [id]); useEffect(function () { if (!instanceRef.current) return; instanceRef.current.setTheme(opts == null ? void 0 : opts.theme); }, [opts == null ? void 0 : opts.theme]); useEffect(function () { if (!instanceRef.current) return; instanceRef.current.setMeta(opts == null ? void 0 : opts.meta); }, [opts == null ? void 0 : opts.meta]); return { open: function open() { var _instanceRef$current; return (_instanceRef$current = instanceRef.current) == null ? void 0 : _instanceRef$current.open(); }, close: function close() { var _instanceRef$current2; return (_instanceRef$current2 = instanceRef.current) == null ? void 0 : _instanceRef$current2.close(); }, toggle: function toggle() { var _instanceRef$current3; return (_instanceRef$current3 = instanceRef.current) == null ? void 0 : _instanceRef$current3.toggle(); } }; }; export default FeedbackButton; export { FeedbackButton, FeedbackWidget, useFeedbackDialog }; //# sourceMappingURL=feedback-react.esm.js.map