Fix: Restore textAlign functionality via ToastContainer prop #1260
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
Fixes #1244
This pull request resolves a regression where the textAlign style was not being applied to toast notifications. The original issue (#657) was reintroduced in a recent version, making it impossible to globally set text alignment for all toasts.
The fix ensures that style properties are correctly applied. For textAlign to work as expected, the toast element must be a block-level element. Therefore, the correct way to implement this is by applying a global toastStyle prop to the ToastContainer.
Changes Made
Ensured that custom styles passed via the toastStyle prop on ToastContainer are correctly applied to every toast it renders.
Updated the approach for text alignment to rely on this global prop for more consistent and predictable CSS behavior.
New Usage for Global Text Alignment
To properly align the text for all toasts rendered by a container, you must now use the toastStyle prop on the component itself. You need to set both display: "block" and the desired textAlign value in the style object.
Example Code:
JavaScript
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
function App() {
// The toast call is now simple, with no style options.
const showToast = () => {
toast("This text is now properly centered!");
};
return (
Show Centered Toast
);
}
This approach provides a reliable way to globally control text alignment for all toasts within a specific container.