Inplace editables - highlighting - interaction

by Mario Winkler

HTML

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<form data-focus-group="" >
    <fieldset class="message">
        <legend>My favorites and hates </legend>
        <div class="wrapper">
            <textarea class="bkg_icon" data-focus-item="" placeholder="Hanging in trees is my favorite sport.">Hanging in trees is my favorite sport.</textarea>
            <i class="glyphicon glyphicon-pencil"></i>
        </div>
        
        <div class="wrapper">
            <textarea class="bkg_icon" data-focus-item="" placeholder="Rocketing to moons - just boring.">Rocketing to moons - just boring.</textarea>
            <i class="glyphicon glyphicon-pencil"></i>
        </div>
    </fieldset>
</form>

CSS

body{
    background-color: #eee;
    padding:20px;
    width: 100%;
}

legend {
    float: left; /*strange clearing needed ...*/
}

.message {
    background-color: #f5f5f5;
    margin: auto;
    width: 90%;
    padding: 1% 10% 5%;
}

.message:hover .inplace-editable {
    border: 2px dashed rgba(0,0,0, 0.1);
}

.message:hover .inplace-editable + .glyphicon  {
    color: rgba(0,0,0, 0.2);
}

.wrapper {
    position: relative;
    display: block;
    width: 100%;
    z-index: 1;
    clear: both;
}

textarea {
    width: 100%;
    height: 100px; 
    margin: auto;
    padding: 8px;
    background: rgba(255,255,255,0.4);
    color: #005f6a; /*petrol*/
    font-size: 1.2em;
    margin-bottom: 1em;
    
    border: 2px dashed rgba(0,0,0, 0.1);
    transition : border 300ms;
    
    -moz-box-sizing: border-box;
         box-sizing: border-box;
    display: block;
    vertical-align: middle;
    overflow: hidden;
    resize: none;
    text-align: center;
    word-wrap: break-word;
    box-shadow: 0 0 0 5px rgba(255, 255, 255, 0.4);
border-radius: 1px;
}

textarea:hover, textarea:focus {
    border-color: #999;
}

textarea:focus, [data-in-focus="true"]{
    outline: none;
    border-color: #e653c1;
}

[data-in-focus="false"]{
    opacity: 0.5;
}

textarea + .glyphicon{
    position: absolute;
    top: 4px;
    right: 4px;
    display: inline-block;
    margin: 4px;
    color: rgba(0,0,0, 0.2);
    transition : color 300ms;
    z-index: -1; /*otherwise - a click on the icon would not set focus on the textarea */
}

textarea:hover + .glyphicon {
    color: rgba(0, 0, 0, 0.6);
}

textarea:focus + .glyphicon{
    color: #e653c1;
}

JavaScript

"strict";

$("[data-focus-item]").parents("[data-focus-group]")
    .on("focus", "[data-focus-item]", function(event){
        var group = $(event.delegateTarget),
            lostFocus = event.type === "blur",
            anyItemFocused = $(group).find(":focus").index();
        
        //if(lostFocus && anyItemFocused) {
        //    $(group).find("[data-focus-item]").removeAttr("data-in-focus");
        //}

        $(group).find("[data-focus-item]").attr("data-in-focus", "false");
        $(this).attr("data-in-focus", "true");
    }
);  

/*$("[data-focus-item]").parents("[data-focus-group]")
    .on("blur", "[data-focus-item]", function(event){
        var group = $(event.delegateTarget);

        $(group).find("[data-focus-item]").attr("data-in-focus", "false");
    }
);  */