Skip to content

Commit 8c19ab5

Browse files
feat: introduce DefaultAlertButton with variant from config
1 parent 455c45e commit 8c19ab5

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

env.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Also note that in an actual application this file would be added to .gitignore.
44
const config = {
55
JS_FILE_VAR: 'JS_FILE_VAR_VALUE_FOR_EXAMPLE_APP',
6+
styleOverrides: {
7+
alertButtonVariant: 'brand',
8+
},
69
};
710

811
export default config;

example/ExamplePage.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { useContext, useEffect } from 'react';
22
import { Link } from 'react-router-dom';
3+
import { Alert } from '@openedx/paragon';
34

45
/* eslint-disable import/no-extraneous-dependencies */
56
import { injectIntl, useIntl } from '@edx/frontend-platform/i18n';
67
import { logInfo } from '@edx/frontend-platform/logging';
7-
import { AppContext } from '@edx/frontend-platform/react';
8+
import { AppContext, DefaultAlertButton } from '@edx/frontend-platform/react';
89
import { ensureConfig, mergeConfig, getConfig } from '@edx/frontend-platform';
910
/* eslint-enable import/no-extraneous-dependencies */
1011
import messages from './messages';
@@ -51,6 +52,16 @@ function ExamplePage() {
5152
<p>JS_FILE_VAR var came through: <strong>{getConfig().JS_FILE_VAR}</strong></p>
5253
<p>Visit <Link to="/authenticated">authenticated page</Link>.</p>
5354
<p>Visit <Link to="/error_example">error page</Link>.</p>
55+
<div>
56+
<Alert
57+
variant="error"
58+
actions={[
59+
<DefaultAlertButton>Hello</DefaultAlertButton>,
60+
]}
61+
>
62+
Lorem ispum dolar sit amet.
63+
</Alert>
64+
</div>
5465
</div>
5566
);
5667
}

src/react/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
* @module React
77
*/
88

9+
export { useAppEvent } from './hooks';
910
export { default as AppContext } from './AppContext';
1011
export { default as AppProvider } from './AppProvider';
1112
export { default as AuthenticatedPageRoute } from './AuthenticatedPageRoute';
1213
export { default as ErrorBoundary } from './ErrorBoundary';
1314
export { default as ErrorPage } from './ErrorPage';
1415
export { default as LoginRedirect } from './LoginRedirect';
1516
export { default as PageWrap } from './PageWrap';
16-
export { useAppEvent } from './hooks';
17+
export { default as DefaultAlertButton } from './paragon/DefaultAlertButton';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { forwardRef } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { Button } from '@openedx/paragon';
4+
import { getConfig } from '../../config';
5+
6+
function useAlertButtonVariant(props) {
7+
if (props.variant) {
8+
// If a variant is explicitly set in the props, use that.
9+
return props.variant;
10+
}
11+
// Otherwise, check for a configured style override for the alert button variant.
12+
const { styleOverrides } = getConfig();
13+
if (styleOverrides?.alertButtonVariant) {
14+
return styleOverrides.alertButtonVariant;
15+
}
16+
17+
// If no variant is set in props and no style override is configured, use Paragon's default variant.
18+
return undefined;
19+
}
20+
21+
/**
22+
* A default alert button component that utilizes a button variant override, if configured. Otherwise,
23+
* it defaults to the ``primary`` button variant. By creating this wrapper component, consuming projects
24+
* can more readily import and use a consistent alert button style across their application without having
25+
* to re-integrate the button variant logic across every usage.
26+
* @memberof module:React
27+
* @param {Object} props
28+
*/
29+
const DefaultAlertButton = forwardRef(({ children, ...props }, ref) => {
30+
const alertButtonVariant = useAlertButtonVariant(props);
31+
return (
32+
<Button
33+
variant={alertButtonVariant}
34+
ref={ref}
35+
{...props}
36+
>
37+
{children}
38+
</Button>
39+
);
40+
});
41+
DefaultAlertButton.displayName = 'DefaultAlertButton';
42+
43+
DefaultAlertButton.propTypes = {
44+
children: PropTypes.node.isRequired,
45+
};
46+
47+
export default DefaultAlertButton;

src/react/paragon/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as DefaultAlertButton } from './DefaultAlertButton';

0 commit comments

Comments
 (0)