Skip to content

Instantly share code, notes, and snippets.

@meriouma
Created July 18, 2013 18:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meriouma/6031865 to your computer and use it in GitHub Desktop.
Save meriouma/6031865 to your computer and use it in GitHub Desktop.
GWT and CSS3 Media queries
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>
<ui:with field="res" type="com.project.client.resource.Resources"/>
<g:HTMLPanel>
<div class="{res.styles.mainContent} {res.mobileStyles.mainContent}">
Some content
</div>
</g:HTMLPanel>
</ui:UiBinder>
public interface Resources extends ClientBundle {
public interface Styles extends CssResource {
String mainContent();
}
public interface MobileStyles extends CssResource {
String mainContent();
}
public Styles styles();
public MobileStyles mobileStyles();
}
public class ResourcesLoader {
private static final int MOBILE_MAX_WIDTH = 700;
private static final String MOBILE_MEDIA = "@media only screen and (max-width: " + MOBILE_MAX_WIDTH + "px), " +
"only screen and (max-device-width: " + MOBILE_MAX_WIDTH + "px) {";
private static final String CLOSING_BRACKET = "}";
@Inject
ResourcesLoader(Resources resources) {
resources.styles().ensureInjected();
injectMediaCss(resources);
}
private void injectMediaCss(Resources resources) {
injectMobileStylesheet(resources.mobileStyles());
}
private void injectMobileStylesheet(CssResource cssResource) {
String mobileCss = wrapMobileStylesheet(cssResource.getText());
StyleInjector.injectAtEnd(mobileCss);
}
private String wrapMobileStylesheet(String content) {
return MOBILE_MEDIA + content + CLOSING_BRACKET;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment