Skip to content
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

Callback when client side less screen is ready #1666

Closed
markrogers123 opened this issue Nov 20, 2013 · 9 comments
Closed

Callback when client side less screen is ready #1666

markrogers123 opened this issue Nov 20, 2013 · 9 comments

Comments

@markrogers123
Copy link

Using client side less, it takes several seconds to render a twitter bootstrap screen.

While less is creating the css, the browser shows unstyled html.

(I use client side less so users can change less options at runtime.)

Is there a js callback or other feature which will let me hide the screen and unhide it when less has finished creating the css (equivalent of document.ready).

If not, can the facility be added?

@lukeapage
Copy link
Member

okay,

  1. you really really should be compiling your less serverside unless you have a very good reason.. then you get it to 0 seconds ;)
  2. we have had requests before but it has never been done. you could submit a pull request, I'd suggest
    adding code here

https://github.com/less/less.js/blob/master/lib/less/browser.js#L673

such that once all requests have come back, it calls less.stylesLoadedCallback if that exists.

@codler
Copy link

codler commented May 18, 2014

I would like to have a callback also so I know when to run a css prefixer library like https://github.com/codler/jQuery-Css3-Finalize

@seven-phases-max
Copy link
Member

@codler Probably that's what #1835 is about. It's already in v1.7.0.

@codler
Copy link

codler commented May 18, 2014

aha nice, thanks! How do I use it? Is it less.postProcessCSS = function() {}? I dont see it in the documentation.

@seven-phases-max
Copy link
Member

Is it less.postProcessCSS = function() {} ?

By the code it looks like the function should accept CSS styles and return processed styles as a string.

@codler
Copy link

codler commented May 24, 2014

Here is the code I use

less = {
    postProcessor: function(css) {
        var processedCSS = css;
        if ($.cssFinalize) {
            $('<textarea>').val(css).cssFinalize({
                'append' : false,
                'callback': function(css) {
                    processedCSS += $.cssFinalize.cssObjToText(css);
                }
            });
        }
        return processedCSS;
    }
};```

@levithomason
Copy link
Contributor

This is fixed in PR #2226. Current state of the PR allows you to recognize when less has finished processing via a promise:

less.hasFinished.then(
    function() {
        // code to run after less has finished
    }
)

This also works with less.refresh() and less.modifyVars().

@TWiStErRob
Copy link

hasFinished from #2226 has been renamed to pageLoadFinished in 8b42ae3

I managed to achieve this in my dev env with Less 2.5.3:

<style type="text/css" id="prevent-fouc">
    body {
        display: none !important;
    }
</style>
<script type="text/javascript">
    less = {
        env: "development",
        async: true
    }
</script>
<!-- include styles and less -->
<script type="text/javascript">
    less.pageLoadFinished.then(function() {
        $('#prevent-fouc').remove();
    });
</script>

Disclaimer: I support not making the user's browser compile the whole Bootstrap or any other CSS framework, it's a horrible idea: just think mobile phones have limited capacity.

@seven-phases-max
Copy link
Member

Closing as added in v2.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants