Skip to content

Commit

Permalink
fix (v8 MessageBar): Add showExpandButton prop to allow manual overri…
Browse files Browse the repository at this point in the history
…de to explicitly show the expand button (#32884)

Co-authored-by: Sarah Higley <smhigley@users.noreply.github.com>
  • Loading branch information
emmayjiang and smhigley authored Sep 30, 2024
1 parent df9de02 commit ee47378
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "fix (v8 MessageBar): Add showExpandButton prop to allow manual override to explicitly show the expand button",
"packageName": "@fluentui/react",
"email": "jiangemma@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Text,
IChoiceGroupStyles,
} from '@fluentui/react';
import { useWindow } from '@fluentui/react-window-provider';

interface IExampleProps {
resetChoice?: () => void;
Expand Down Expand Up @@ -106,24 +107,65 @@ const SuccessExample = () => (
</MessageBar>
);

const WarningExample = (p: IExampleProps) => (
<MessageBar
messageBarType={MessageBarType.warning}
isMultiline={false}
onDismiss={p.resetChoice}
dismissButtonAriaLabel="Close"
actions={
<div>
<MessageBarButton>Action</MessageBarButton>
</div>
const WarningExample = (p: IExampleProps) => {
const [showExpandButton, setShowExpandButton] = React.useState(false);

function onResize() {
const messageBarMessage = document.getElementById('warningMessageBar') as HTMLDivElement;

if (messageBarMessage) {
const temp = messageBarMessage.cloneNode(true) as any;

temp.style.position = 'fixed';
temp.style.overflow = 'visible';
temp.style.whiteSpace = 'nowrap';
temp.style.visibility = 'hidden';

(messageBarMessage.parentElement as any).appendChild(temp);

const fullWidth = temp.getBoundingClientRect().width;
const displayWidth = messageBarMessage.getBoundingClientRect().width;

setShowExpandButton(fullWidth > displayWidth);

(messageBarMessage.parentElement as any).removeChild(temp);
}
>
Warning MessageBar content.
<Link href="www.bing.com" target="_blank" underline>
Visit our website.
</Link>
</MessageBar>
);
}

const win = useWindow();
React.useEffect(() => {
// The setTimeout is needed for the initial render because the
// contents of the MessageBar is wrapped in a DelayedRender.
setTimeout(onResize, 0);

win && win.addEventListener('resize', onResize);

return () => {
win && win.removeEventListener('resize', onResize);
};
}, [win]);

return (
<MessageBar
id="warningMessageBar"
messageBarType={MessageBarType.warning}
isMultiline={false}
onDismiss={p.resetChoice}
dismissButtonAriaLabel="Close"
actions={
<div>
<MessageBarButton>Action</MessageBarButton>
</div>
}
showExpandButton={showExpandButton}
>
Warning MessageBar content.
<Link href="www.bing.com" target="_blank" underline>
Visit our website.
</Link>
</MessageBar>
);
};

const WarningExample2 = (p: IExampleProps) => (
<MessageBar
Expand Down
1 change: 1 addition & 0 deletions packages/react/etc/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7026,6 +7026,7 @@ export interface IMessageBarProps extends React_2.HTMLAttributes<HTMLElement>, R
// @deprecated
overflowButtonAriaLabel?: string;
role?: 'alert' | 'status' | 'none';
showExpandButton?: boolean;
styles?: IStyleFunctionOrObject<IMessageBarStyleProps, IMessageBarStyles>;
theme?: ITheme;
truncated?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/MessageBar/MessageBar.base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const MessageBarBase: React.FunctionComponent<IMessageBarProps> = React.f
delayedRender = true,
expandButtonProps,
onExpandButtonToggled = undefined,
showExpandButton,
} = props;

// Wrap 'toggleExpandSingleLine' to execute the 'onExpandButtonToggled' callback whenever the expand button toggles
Expand Down Expand Up @@ -137,7 +138,7 @@ export const MessageBarBase: React.FunctionComponent<IMessageBarProps> = React.f
</span>
</div>
{
/* singleline expand/collapse button */ !isMultiline && !actionsDiv && truncated && (
/* singleline expand/collapse button */ (showExpandButton || (!isMultiline && !actionsDiv && truncated)) && (
<div className={classNames.expandSingleLine}>
<IconButton
disabled={false}
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/components/MessageBar/MessageBar.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export interface IMessageBarProps extends React.HTMLAttributes<HTMLElement>, Rea
* @default true
*/
delayedRender?: boolean;

/**
* An optional override to show the expand/collapse icon. It will only be shown by default for
* single-line truncated MessageBars that do not have actions.
* @defaultvalue false
*/
showExpandButton?: boolean;
}

/**
Expand Down

0 comments on commit ee47378

Please sign in to comment.