-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ChangeTheme: Sometimes leaves 2 or more links in the <head>
#6476
ChangeTheme: Sometimes leaves 2 or more links in the <head>
#6476
Comments
I don't really understand what exactly you're doing there. However, I don't think that this is something PrimeReact could fix. Why are you doing things so complicated? Just do something like: <link id="theme" rel="stylesheet" type="text/css" href="/my/default/theme.css"> let themeLink = document.getElementById('theme');
if (themeLink) {
themeLink.href = `/themes/${themeId}/theme.css`;
} |
am changing between dark and light theme following prime react docs your solution was what i tried first but the problem with it during transition or the process of changing the theme you can see the elements unstyled for like 2 seconds before a theme gets updated which makes it wrong approach i think |
This comment was marked as off-topic.
This comment was marked as off-topic.
@bloodykheeng so you are saying it has 2 |
@melloware i can reproduce it on the doc. However, it took a while to switch themes over and over and it felt like i was too fast one time which causes this |
ahhh it might be a timing thing with the hooks and order its firing... |
bloodykheeng may work in strict mode which causes double render then? Edit: |
<head>
<head>
<head>
@bloodykheeng you're right - if you dont have any fallback theme, the switch can cause a moment of unstyle thing because the css is loaded over the network |
@sja-cslab let me know if we need to fix the code or update the docs or both. |
@melloware @sja-cslab What i noted is that every time i call this changeTheme it creates a link element in the head of the document if u have only one function that calls changeTheme every thing works fine but if u call it in another place that will invoke two links in that head thats why in my code i had to put a call back that manually removes one link from the head see the call back in the above function i use that function to get the theme i stored in local storage on initial mount so it gets it and sets it fine but now after that i have this other one too
the above changes the theme everytime user clicks on a button in the app so i notted that using two changeTheme hooks in my context creates two links of the same id in the head which causes an accident when the app css styles are running it ends up that this changeMyTheme() will never work correctly because it will be changing the first link so that why i said i had to manually remove a one link from the head section on intial load after applying the theme from users local storage by doing this here is my entire code i had placed in the context `import React, { const ThemeContext = createContext(); export const ThemeProvider = ({ children }) => { // const [theme, setTheme] = useState("dark"); const getThemeFromLocalStorage = () => { const changeMyTheme = () => { let myTheme = theme; useEffect(() => { const toggleTheme = () => {
}; return ( export const useTheme = () => useContext(ThemeContext); |
@melloware, the docs are just fine - I would say it's a bug. And yeah, it seems to be a timing problem. The following happens: 1: Find the link element. The issue arises with steps 5 and 6. If a second clone is created and inserted right before the loaded event triggers, we end up with two clones, both of which are renamed to the original ID. Question here is, how would you like to prevent that? We could check for an already available clone e.g. // Check if a cloned link element already exists
const existingCloneElement = document.getElementById(linkElementId + '-clone');
// If a cloned link element already exists, remove it from the document
existingCloneElement?.remove(); |
@sja-cslab well i think u should make sure after updating the link path then either delete the original or the clone such that we don't end up with two links of same id pointing to same css styles |
@bloodykheeng if you delete the original element before the load, you'll end up with the effect, that you got a timespan without styling |
@sja-cslab see how i did it by deleting the parent in the frrom the changeTheme call back then is the whole
|
I see what you're doing there, but you can't be sure that this would always work. As you say in the comment, "Assuming," which should not be used here. Because in different timing/callchain scenarios, this could still cause the wrong theme to load. So it's better to avoid that as soon as possible. In your example, let's say you load theme PS: I'm not sure if that example is correct but I think it explains what could happen |
@sja-cslab ur right i think what prime react team members should do is to change the change theme logic in that were as it clones let it alwas delete the first link that is at index 0 if it exists or else lets change this strategy of manipulating the dom and just apply a layer in some config.js like how tailwind does |
Hii i am storing the theme in my db,,when i get it from the server i want to change the theme , the colorscheme gets changed but the theme color remains the same...tell me where i am doing wrong? @nitrogenous can you help? const { data: currentUserTheme } = useSpecificUserTheme();
|
|
@rahulAnand1999 you change it including the color scheme like.. changeTheme('lara-light-blue', 'bootstrap4-dark-purple', 'theme-link', () => {
setLayoutConfig(currentUserTheme);
}); The them URLs look like...
You are finding and replacing the entire 4th section of that url. |
@bloodykheeng @sja-cslab @rahulAnand1999 submitted a PR to improve the docs as well not do an insert and delete link but just replace the link in place. It seems to be working using the showcase. |
Describe the bug
ive noted that everytime i call change theme it duplicates the link element am refereing to in the head section so which causes me issuses when am changing the theme though i had the manually first always delete exsting nodes by doing this
if (existingLinks.length > 1) { // Assuming that the first link is the old one and the new one is appended last, // we remove the first occurrence. document.head.removeChild(existingLinks[0]); } });
here is my whole code
here is the link element am refering two in the dom head section
i dono if this is a bug or what but i hope this get solved.
Reproducer
No response
PrimeReact version
10.6.2
React version
17.x
Language
ES6
Build / Runtime
Create React App (CRA)
Browser(s)
chrome
Steps to reproduce the behavior
No response
Expected behavior
I expect the change theme to just reference to the link element in the head and it changes the path not creating new link element every time i call it in a different function
The text was updated successfully, but these errors were encountered: