Skip to content

Commit

Permalink
fix: fix build-docs issue, refactor toast component
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrylo Hudym-Levkovych authored and Kyrylo Hudym-Levkovych committed Jan 2, 2024
1 parent 5833ac0 commit 4276858
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Toast/ToastContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ToastContainer({ position, className }) {
const portalDivRef = useRef(null);
const positionStyle = positionStyles[position] || positionStyles['bottom-left'];

if (!portalDivRef.current) {
if (!portalDivRef.current && typeof document !== 'undefined') {
portalDivRef.current = document.createElement('div');
portalDivRef.current.setAttribute('class', 'toast-portal');
portalDivRef.current.setAttribute('role', 'alert');
Expand Down
13 changes: 3 additions & 10 deletions www/src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import HipsterIpsum from './exampleComponents/HipsterIpsum';
import ExamplePropsForm from './exampleComponents/ExamplePropsForm';

const {
Collapsible, Toast, IconButton, Icon,
Collapsible, IconButton, Icon, toast, ToastContainer
} = ParagonReact;

export type CollapsibleLiveEditorTypes = {
Expand Down Expand Up @@ -125,14 +125,13 @@ function CodeBlock({
}: ICodeBlock) {
const intl = useIntl();
const language: any = className ? className.replace(/language-/, '') : 'jsx';
const [showToast, setShowToast] = useState(false);
const [codeExample, setCodeExample] = useState(children);

const handleCodeChange = (e) => setCodeExample(e.target.value);

const handleCopyCodeExample = () => {
navigator.clipboard.writeText(codeExample);
setShowToast(true);
toast({message: 'Code example copied to clipboard!', duration: 2000 });
};

if (live) {
Expand Down Expand Up @@ -168,13 +167,7 @@ function CodeBlock({
</CollapsibleLiveEditor>
<LiveError className="pgn-doc__code-block-error" />
</LiveProvider>
<Toast
onClose={() => setShowToast(false)}
show={showToast}
delay={2000}
>
Code example copied to clipboard!
</Toast>
<ToastContainer />
</div>
);
}
Expand Down
13 changes: 3 additions & 10 deletions www/src/components/IconsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import debounce from 'lodash.debounce';
import { Icon, SearchField, Toast } from '~paragon-react';
import { Icon, SearchField, ToastContainer, toast } from '~paragon-react';
import * as IconComponents from '~paragon-icons';
import { ICON_COPIED_EVENT, sendUserAnalyticsEvent } from '../../segment-events';

Expand Down Expand Up @@ -67,15 +67,14 @@ function IconsTable({ iconNames }) {
const [tableWidth, setTableWidth] = useState(0);
const [data, setData] = useState({ iconsList: iconNames, rowsCount: ROWS_PER_WINDOW });
const [currentIcon, setCurrentIcon] = useState(iconNames[0]);
const [showToast, setShowToast] = useState(false);
const currentIconImport = `import { ${currentIcon} } from '@openedx/paragon/icons';`;
const { rowsCount, iconsList } = data;

const columnsCount = useMemo(() => Math.floor(tableWidth / COLUMN_WIDTH), [tableWidth]);

const copyToClipboard = (content) => {
navigator.clipboard.writeText(content);
setShowToast(true);
toast({ message: 'Copied to clipboard!', duration: 2000 });
sendUserAnalyticsEvent(ICON_COPIED_EVENT, { name: currentIcon });
};

Expand Down Expand Up @@ -178,13 +177,7 @@ function IconsTable({ iconNames }) {
</div>

<div ref={tableBottom} />
<Toast
onClose={() => setShowToast(false)}
show={showToast}
delay={2000}
>
Copied to clipboard!
</Toast>
<ToastContainer />
</>
);
}
Expand Down
16 changes: 4 additions & 12 deletions www/src/pages/foundations/elevation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
Button,
Form,
Input,
Toast,
ToastContainer,
toast,
Icon,
IconButtonWithTooltip,
} from '~paragon-react';
Expand All @@ -28,11 +29,9 @@ const controlsProps = [
];

function BoxShadowNode() {
const [showToast, setShowToast] = useState(false);

const isBoxShadowCopied = (level, side) => {
navigator.clipboard.writeText(`@include pgn-box-shadow(${level}, "${side}");`);
setShowToast(true);
toast({ message: 'Box-shadow copied to clipboard!', duration: 2000 });
};

const boxShadowCells = boxShadowLevels.map(level => (
Expand All @@ -56,14 +55,7 @@ function BoxShadowNode() {
return (
<div className="pgn-doc__box-shadow-cells">
{ boxShadowCells }
<Toast
className="pgn-doc__box-shadow--toast"
onClose={() => setShowToast(false)}
show={showToast}
delay={2000}
>
Box-shadow copied to clipboard!
</Toast>
<ToastContainer />
</div>
);
}
Expand Down

0 comments on commit 4276858

Please sign in to comment.