All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.patternfly.component.brand.Brand Maven / Gradle / Ivy

There is a newer version: 0.2.11
Show newest version
/*
 *  Copyright 2023 Red Hat
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.patternfly.component.brand;

import org.jboss.elemento.HTMLElementBuilder;
import org.jboss.elemento.logger.Logger;
import org.patternfly.component.BaseComponent;
import org.patternfly.component.ComponentType;
import org.patternfly.style.Breakpoints;
import org.patternfly.style.Classes;

import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLImageElement;

import static org.jboss.elemento.Elements.img;
import static org.jboss.elemento.Elements.picture;
import static org.jboss.elemento.Elements.source;
import static org.patternfly.style.Breakpoint.default_;
import static org.patternfly.style.Breakpoints.breakpoints;
import static org.patternfly.style.Classes.brand;
import static org.patternfly.style.Classes.component;
import static org.patternfly.style.Classes.modifier;
import static org.patternfly.style.Variable.componentVar;
import static org.patternfly.style.Variables.Height;
import static org.patternfly.style.Variables.Width;

/**
 * A brand is used to place a product logotype on a screen.
 *
 * @see https://www.patternfly.org/components/brand
 */
public class Brand extends BaseComponent {

    // ------------------------------------------------------ factory

    public static Brand brand(String src, String alt) {
        return new Brand(img().element(), src, alt);
    }

    public static Brand brand() {
        return new Brand(picture().element(), null, null);
    }

    // ------------------------------------------------------ instance

    private static final Logger logger = Logger.getLogger(Brand.class.getName());
    private final boolean picture;

     Brand(E element, String src, String alt) {
        super(ComponentType.Brand, element);
        css(component(brand));
        picture = src == null;
        if (picture) {
            css(modifier(Classes.picture));
        } else {
            img(element).apply(i -> {
                i.src = src;
                i.alt = alt;
            }).element();
        }
    }

    // ------------------------------------------------------ add

    public Brand addSource(String src) {
        return addSource(src, null);
    }

    public Brand addSource(String src, String media) {
        if (picture) {
            add(source().apply(s -> {
                s.srcset = src;
                if (media != null) {
                    s.media = media;
                }
            }));
        } else {
            logger.warn("Brand %o contains a  element. Adding sources is not supported.", element());
        }
        return this;
    }

    public Brand addImg(HTMLElementBuilder img) {
        return add(img);
    }

    public Brand add(HTMLElementBuilder img) {
        if (picture) {
            add(img.element());
        } else {
            logger.warn("Brand %o contains a  element. Adding a fallback image is not supported.", element());
        }
        return this;
    }

    // ------------------------------------------------------ builder

    /**
     * Same as {@code widths(breakpoints(default_, widths))}
     */
    public Brand widths(String widths) {
        return widths(breakpoints(default_, widths));
    }

    public Brand widths(Breakpoints widths) {
        return componentVar(component(brand), Width).applyTo(this).set(widths);
    }

    /**
     * Same as {@code heights(breakpoints(default_, heights))}
     */
    public Brand heights(String heights) {
        return heights(breakpoints(default_, heights));
    }

    public Brand heights(Breakpoints heights) {
        return componentVar(component(brand), Height).applyTo(this).set(heights);
    }

    @Override
    public Brand that() {
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy